Skip to content

Commit

Permalink
Merge pull request matthewsamuel95#573 from anirudherabelly/master
Browse files Browse the repository at this point in the history
added problems for Heap and Stack
  • Loading branch information
matthewsamuel95 authored Oct 15, 2018
2 parents 0d79652 + a27972e commit 530ec7a
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Data Structures/Heap/RunningMedian/RunningMedian_V1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Given an input stream of n integers the task is to insert integers to stream and
print the median of the new stream formed by each insertion of x to the stream.
--------------------------------------------------------------------------------
Example
Flow in stream : 5, 15, 1, 3
5 goes to stream --> median 5 (5)
15 goes to stream --> median 10 (5, 15)
1 goes to stream --> median 5 (5, 15, 1)
3 goes to stream --> median 4 (5, 15, 1, 3)
Input:
The first line of input contains an integer N denoting the no of elements of the stream.
Then the next N lines contains integer x denoting the no to be inserted to the stream.
Output:
For each element added to the stream print the floor of the new median in a new line.
Constraints:
1<=N<=10^5+7
1<=x<=10^5+7
Example:
Input:
4
5
15
1
3
Output:
5
10
5
4
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
//code
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
PriorityQueue<Integer> maxHeap=new PriorityQueue<Integer>(
new Comparator<Integer> () {
public int compare(Integer a, Integer b) {
return b - a;
}
}
);
PriorityQueue<Integer> minHeap=new PriorityQueue<Integer>();
int first=sc.nextInt();
int second=sc.nextInt();
if(first<second){
maxHeap.add(first);
minHeap.add(second);
}
else{
maxHeap.add(second);
minHeap.add(first);
}
System.out.println(first);
System.out.println(((first+second))/2);
for(int i=0;i<n-2;i++){
int t=sc.nextInt();
if(t<maxHeap.peek())maxHeap.add(t);
else minHeap.add(t);
int diff=minHeap.size()-maxHeap.size();
if(diff>1)maxHeap.add(minHeap.poll());
if(diff<-1)minHeap.add(maxHeap.poll());
diff=minHeap.size()-maxHeap.size();
if(diff==0)System.out.println((minHeap.peek()+maxHeap.peek())/2);
else if(diff==1)System.out.println(minHeap.peek());
else System.out.println(maxHeap.peek());
}
}
}
46 changes: 46 additions & 0 deletions Data Structures/Heap/RunningMedian/RunningMedian_V2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class MedianFinder {

/** initialize your data structure here. */
PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(
new Comparator<Integer>(){
public int compare(Integer a, Integer b){
if(a > b)return -1;
else if(a == b)return 0;
return 1;
}
}
);

public MedianFinder() {

}

public void addNum(int num) {
int diff = maxHeap.size()-minHeap.size();
if(diff == 0){
minHeap.add(num);
maxHeap.add(minHeap.poll());
}
else{
maxHeap.add(num);
minHeap.add(maxHeap.poll());
}
}

public double findMedian() {
double median = 0;
int diff = maxHeap.size()-minHeap.size();

if(diff==0)median = (maxHeap.peek()+minHeap.peek())/2.0;
else median = maxHeap.peek();
return median;
}
}

/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/
28 changes: 28 additions & 0 deletions Data Structures/Stack/BalancedParanthesis/BalancedParanthesis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

static void Main(String[] args) {
int t = Convert.ToInt32(Console.ReadLine());
for(int a0 = 0; a0 < t; a0++){
string s = Console.ReadLine();
Console.WriteLine(CheckBalanced(s));
}
}
static string CheckBalanced(string s){
Stack<char> st=new Stack<char>();
foreach(char c in s){
if(c=='[' || c=='{' || c=='(')st.Push(c);
else{
if(st.Count==0)return "NO";
char ch=st.Pop();
if((ch=='(' && c==')')||(ch=='[' && c==']')||(ch=='{' && c=='}'))continue;
else return "NO";
}
}
if(st.Count!=0)return "NO";
return "YES";
}
}
37 changes: 37 additions & 0 deletions Data Structures/Stack/MaxHistogramArea/MaxHistogramArea.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int n = int.Parse(Console.ReadLine());
int[] array = Array.ConvertAll(Console.ReadLine().Split(' '),int.Parse);
LargestRectangle(array,n);
}
private static void LargestRectangle(int[] array, int n)
{
Stack<int> st = new Stack<int>();
int i = 0,max_area=-1,area=0;
while (i < n)
{
if (st.Count == 0 || array[st.Peek()] <= array[i])
{
st.Push(i);
i++;
}
else
{
int top = array[st.Pop()];
area = top* ((st.Count == 0) ? i : i-st.Peek()-1);
if (area > max_area) max_area = area;
}
}
while (st.Count != 0)
{
int top = array[st.Pop()];
area = top * ((st.Count == 0) ? i : i - st.Peek() - 1);
if (area > max_area) max_area = area;
}
Console.WriteLine(max_area);
}
}
47 changes: 47 additions & 0 deletions Data Structures/Stack/MinStack/MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class MinStack {
private class Node{
int val, min;
Node next;
Node(int val, int min, Node next){
this.val = val;
this.min = min;
this.next = next;
}
}
private Node head;

/** initialize your data structure here. */
public MinStack() {
head = null;
}

public void push(int x) {
if(head == null){
head = new Node(x, x, null);
}
else{
head = new Node(x, Math.min(x, head.min), head);
}
}

public void pop() {
head = head.next;
}

public int top() {
return head.val;
}

public int getMin() {
return head.min;
}
}

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/

0 comments on commit 530ec7a

Please sign in to comment.