diff --git a/MinStack.cs b/MinStack.cs new file mode 100644 index 00000000..dafde2bf --- /dev/null +++ b/MinStack.cs @@ -0,0 +1,61 @@ +// Time Complexity : O(1) +// Space Complexity :O(n) - Extra stacks +// Did this code successfully run on Leetcode : Yes + +// Any problem you faced while coding this : +//NA + +// Your code here along with comments explaining your approach +// I have maintained two stack one is original and other one is Min stack +// I will add vals in stack and for min stack compare the values like if the new value is less than top element of the stack then only insert it in the min stack else insert last min value. + +public class MinStack +{ + + private Stack stack; + private Stack minStack; + public MinStack() + { + stack = new Stack(); + minStack = new Stack(); + } + + public void Push(int val) + { + stack.Push(val); + + if (minStack.Count == 0 || val < minStack.Peek()) + { + minStack.Push(val); + } + else + { + minStack.Push(minStack.Peek()); + } + } + + public void Pop() + { + stack.Pop(); + minStack.Pop(); + } + + public int Top() + { + return stack.Peek(); + } + + public int GetMin() + { + return minStack.Peek(); + } +} + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.Push(val); + * obj.Pop(); + * int param_3 = obj.Top(); + * int param_4 = obj.GetMin(); + */ \ No newline at end of file diff --git a/Sample.cs b/Sample.cs new file mode 100644 index 00000000..54dc64ec --- /dev/null +++ b/Sample.cs @@ -0,0 +1,56 @@ +// Time Complexity : O(1) +// Space Complexity :O(n) - 1bit is allocated per key +// Did this code successfully run on Leetcode : Yes + +// Any problem you faced while coding this : + +//Yes I had to spend some time on understanding how the hashing works, I havent yet implemented solution with Linkedlist and hashing technique. + +// Your code here along with comments explaining your approach +// I have used BitArray since it slightly saves your memory than bool[] , +// if the size of the data is unknown then array is not the feasible option +// we should be using linkedlist and hashing techniques +//but to do that I need some time to understand that concept so I will take some time to implement that approach. + + +public class MyHashSet +{ + //bool[] arr; + BitArray arr; + public MyHashSet() + { + // arr = new bool[1_000_001]; + arr = new BitArray(1000001); + } + + public void Add(int key) + { + arr[key] = true; + } + + public void Remove(int key) + { + arr[key] = false; + } + + public bool Contains(int key) + { + return arr[key]; + } +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.Add(key); + * obj.Remove(key); + * bool param_3 = obj.Contains(key); + */ + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.Add(key); + * obj.Remove(key); + * bool param_3 = obj.Contains(key); + */ diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cb..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach