From cdaf9eab8bf8d8d7444d4743afbc8615db9b6793 Mon Sep 17 00:00:00 2001 From: anirudherabelly Date: Sun, 14 Oct 2018 23:08:36 -0500 Subject: [PATCH 1/3] added important problems for stacks. --- .../BalancedParanthesis.cs | 28 +++++++++++ .../MaxHistogramArea/MaxHistogramArea.cs | 37 +++++++++++++++ Data Structures/Stack/MinStack/MinStack.java | 47 +++++++++++++++++++ README.md | 1 + 4 files changed, 113 insertions(+) create mode 100644 Data Structures/Stack/BalancedParanthesis/BalancedParanthesis.cs create mode 100644 Data Structures/Stack/MaxHistogramArea/MaxHistogramArea.cs create mode 100644 Data Structures/Stack/MinStack/MinStack.java diff --git a/Data Structures/Stack/BalancedParanthesis/BalancedParanthesis.cs b/Data Structures/Stack/BalancedParanthesis/BalancedParanthesis.cs new file mode 100644 index 00000000..17f84bc8 --- /dev/null +++ b/Data Structures/Stack/BalancedParanthesis/BalancedParanthesis.cs @@ -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 st=new Stack(); + 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"; + } +} \ No newline at end of file diff --git a/Data Structures/Stack/MaxHistogramArea/MaxHistogramArea.cs b/Data Structures/Stack/MaxHistogramArea/MaxHistogramArea.cs new file mode 100644 index 00000000..13679fbe --- /dev/null +++ b/Data Structures/Stack/MaxHistogramArea/MaxHistogramArea.cs @@ -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 st = new Stack(); + 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); + } +} \ No newline at end of file diff --git a/Data Structures/Stack/MinStack/MinStack.java b/Data Structures/Stack/MinStack/MinStack.java new file mode 100644 index 00000000..a96c89ff --- /dev/null +++ b/Data Structures/Stack/MinStack/MinStack.java @@ -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(); + */ \ No newline at end of file diff --git a/README.md b/README.md index 338ffea2..5c27395e 100755 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ ACM-ICPC Algorithms is a collection of important algorithms and data structures - C - C++ - Java +- c# - Python (2 & 3) - Kotlin. ##### For more information, visit: **Official Website of [ICPC](https://icpc.baylor.edu/)** From 11e501de0fb882646f9442e9c24345b97483e21a Mon Sep 17 00:00:00 2001 From: anirudherabelly Date: Mon, 15 Oct 2018 12:27:52 -0500 Subject: [PATCH 2/3] Added running median heap problem --- .../Heap/RunningMedian/RunningMedian_V1.java | 80 +++++++++++++++++++ .../Heap/RunningMedian/RunningMedian_V2.java | 46 +++++++++++ 2 files changed, 126 insertions(+) create mode 100644 Data Structures/Heap/RunningMedian/RunningMedian_V1.java create mode 100644 Data Structures/Heap/RunningMedian/RunningMedian_V2.java diff --git a/Data Structures/Heap/RunningMedian/RunningMedian_V1.java b/Data Structures/Heap/RunningMedian/RunningMedian_V1.java new file mode 100644 index 00000000..91abf757 --- /dev/null +++ b/Data Structures/Heap/RunningMedian/RunningMedian_V1.java @@ -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 maxHeap=new PriorityQueue( + new Comparator () { + public int compare(Integer a, Integer b) { + return b - a; + } + } + ); + PriorityQueue minHeap=new PriorityQueue(); + int first=sc.nextInt(); + int second=sc.nextInt(); + if(first1)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()); + } + } +} diff --git a/Data Structures/Heap/RunningMedian/RunningMedian_V2.java b/Data Structures/Heap/RunningMedian/RunningMedian_V2.java new file mode 100644 index 00000000..626a247a --- /dev/null +++ b/Data Structures/Heap/RunningMedian/RunningMedian_V2.java @@ -0,0 +1,46 @@ +class MedianFinder { + + /** initialize your data structure here. */ + PriorityQueue minHeap = new PriorityQueue(); + PriorityQueue maxHeap = new PriorityQueue( + new Comparator(){ + 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(); + */ \ No newline at end of file From a27972e98c7d7cdfb4f190cabad7368d3d6ff3f6 Mon Sep 17 00:00:00 2001 From: anirudherabelly Date: Mon, 15 Oct 2018 12:30:26 -0500 Subject: [PATCH 3/3] reverted changes for readme.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 5c27395e..338ffea2 100755 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ ACM-ICPC Algorithms is a collection of important algorithms and data structures - C - C++ - Java -- c# - Python (2 & 3) - Kotlin. ##### For more information, visit: **Official Website of [ICPC](https://icpc.baylor.edu/)**