Skip to content

Commit 530ec7a

Browse files
Merge pull request #573 from anirudherabelly/master
added problems for Heap and Stack
2 parents 0d79652 + a27972e commit 530ec7a

5 files changed

Lines changed: 238 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Given an input stream of n integers the task is to insert integers to stream and
3+
print the median of the new stream formed by each insertion of x to the stream.
4+
--------------------------------------------------------------------------------
5+
Example
6+
7+
Flow in stream : 5, 15, 1, 3
8+
5 goes to stream --> median 5 (5)
9+
15 goes to stream --> median 10 (5, 15)
10+
1 goes to stream --> median 5 (5, 15, 1)
11+
3 goes to stream --> median 4 (5, 15, 1, 3)
12+
13+
Input:
14+
The first line of input contains an integer N denoting the no of elements of the stream.
15+
Then the next N lines contains integer x denoting the no to be inserted to the stream.
16+
17+
Output:
18+
For each element added to the stream print the floor of the new median in a new line.
19+
20+
Constraints:
21+
1<=N<=10^5+7
22+
1<=x<=10^5+7
23+
24+
Example:
25+
Input:
26+
4
27+
5
28+
15
29+
1
30+
3
31+
Output:
32+
5
33+
10
34+
5
35+
4
36+
*/
37+
import java.util.*;
38+
import java.lang.*;
39+
import java.io.*;
40+
class GFG
41+
{
42+
public static void main (String[] args)
43+
{
44+
//code
45+
Scanner sc=new Scanner(System.in);
46+
int n=sc.nextInt();
47+
PriorityQueue<Integer> maxHeap=new PriorityQueue<Integer>(
48+
new Comparator<Integer> () {
49+
public int compare(Integer a, Integer b) {
50+
return b - a;
51+
}
52+
}
53+
);
54+
PriorityQueue<Integer> minHeap=new PriorityQueue<Integer>();
55+
int first=sc.nextInt();
56+
int second=sc.nextInt();
57+
if(first<second){
58+
maxHeap.add(first);
59+
minHeap.add(second);
60+
}
61+
else{
62+
maxHeap.add(second);
63+
minHeap.add(first);
64+
}
65+
System.out.println(first);
66+
System.out.println(((first+second))/2);
67+
for(int i=0;i<n-2;i++){
68+
int t=sc.nextInt();
69+
if(t<maxHeap.peek())maxHeap.add(t);
70+
else minHeap.add(t);
71+
int diff=minHeap.size()-maxHeap.size();
72+
if(diff>1)maxHeap.add(minHeap.poll());
73+
if(diff<-1)minHeap.add(maxHeap.poll());
74+
diff=minHeap.size()-maxHeap.size();
75+
if(diff==0)System.out.println((minHeap.peek()+maxHeap.peek())/2);
76+
else if(diff==1)System.out.println(minHeap.peek());
77+
else System.out.println(maxHeap.peek());
78+
}
79+
}
80+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class MedianFinder {
2+
3+
/** initialize your data structure here. */
4+
PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
5+
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(
6+
new Comparator<Integer>(){
7+
public int compare(Integer a, Integer b){
8+
if(a > b)return -1;
9+
else if(a == b)return 0;
10+
return 1;
11+
}
12+
}
13+
);
14+
15+
public MedianFinder() {
16+
17+
}
18+
19+
public void addNum(int num) {
20+
int diff = maxHeap.size()-minHeap.size();
21+
if(diff == 0){
22+
minHeap.add(num);
23+
maxHeap.add(minHeap.poll());
24+
}
25+
else{
26+
maxHeap.add(num);
27+
minHeap.add(maxHeap.poll());
28+
}
29+
}
30+
31+
public double findMedian() {
32+
double median = 0;
33+
int diff = maxHeap.size()-minHeap.size();
34+
35+
if(diff==0)median = (maxHeap.peek()+minHeap.peek())/2.0;
36+
else median = maxHeap.peek();
37+
return median;
38+
}
39+
}
40+
41+
/**
42+
* Your MedianFinder object will be instantiated and called as such:
43+
* MedianFinder obj = new MedianFinder();
44+
* obj.addNum(num);
45+
* double param_2 = obj.findMedian();
46+
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
class Solution {
6+
7+
static void Main(String[] args) {
8+
int t = Convert.ToInt32(Console.ReadLine());
9+
for(int a0 = 0; a0 < t; a0++){
10+
string s = Console.ReadLine();
11+
Console.WriteLine(CheckBalanced(s));
12+
}
13+
}
14+
static string CheckBalanced(string s){
15+
Stack<char> st=new Stack<char>();
16+
foreach(char c in s){
17+
if(c=='[' || c=='{' || c=='(')st.Push(c);
18+
else{
19+
if(st.Count==0)return "NO";
20+
char ch=st.Pop();
21+
if((ch=='(' && c==')')||(ch=='[' && c==']')||(ch=='{' && c=='}'))continue;
22+
else return "NO";
23+
}
24+
}
25+
if(st.Count!=0)return "NO";
26+
return "YES";
27+
}
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
class Solution {
5+
static void Main(String[] args) {
6+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
7+
int n = int.Parse(Console.ReadLine());
8+
int[] array = Array.ConvertAll(Console.ReadLine().Split(' '),int.Parse);
9+
LargestRectangle(array,n);
10+
}
11+
private static void LargestRectangle(int[] array, int n)
12+
{
13+
Stack<int> st = new Stack<int>();
14+
int i = 0,max_area=-1,area=0;
15+
while (i < n)
16+
{
17+
if (st.Count == 0 || array[st.Peek()] <= array[i])
18+
{
19+
st.Push(i);
20+
i++;
21+
}
22+
else
23+
{
24+
int top = array[st.Pop()];
25+
area = top* ((st.Count == 0) ? i : i-st.Peek()-1);
26+
if (area > max_area) max_area = area;
27+
}
28+
}
29+
while (st.Count != 0)
30+
{
31+
int top = array[st.Pop()];
32+
area = top * ((st.Count == 0) ? i : i - st.Peek() - 1);
33+
if (area > max_area) max_area = area;
34+
}
35+
Console.WriteLine(max_area);
36+
}
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class MinStack {
2+
private class Node{
3+
int val, min;
4+
Node next;
5+
Node(int val, int min, Node next){
6+
this.val = val;
7+
this.min = min;
8+
this.next = next;
9+
}
10+
}
11+
private Node head;
12+
13+
/** initialize your data structure here. */
14+
public MinStack() {
15+
head = null;
16+
}
17+
18+
public void push(int x) {
19+
if(head == null){
20+
head = new Node(x, x, null);
21+
}
22+
else{
23+
head = new Node(x, Math.min(x, head.min), head);
24+
}
25+
}
26+
27+
public void pop() {
28+
head = head.next;
29+
}
30+
31+
public int top() {
32+
return head.val;
33+
}
34+
35+
public int getMin() {
36+
return head.min;
37+
}
38+
}
39+
40+
/**
41+
* Your MinStack object will be instantiated and called as such:
42+
* MinStack obj = new MinStack();
43+
* obj.push(x);
44+
* obj.pop();
45+
* int param_3 = obj.top();
46+
* int param_4 = obj.getMin();
47+
*/

0 commit comments

Comments
 (0)