diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 00000000..87a6616d --- /dev/null +++ b/MinStack.java @@ -0,0 +1,55 @@ +// Time Complexity : O(1) for all operations +// Space Complexity : O(n) where n is the number of elements in the stack +// Did this code successfully run on Leetcode : Yes +// Approach: I am using 2 stacks. One stack is used to store the elements and the other stack is used to store the minimum element at that point. I am pushing the element to the stack and also pushing the minimum element to the minStack. If the element is removed from the stack, I am removing the element from the stack and also removing the element from the minStack. If the minStack is empty, I am setting the min value to Integer.MAX_VALUE. If the minStack is not empty, I am setting the min value to the top element of the minStack. + +import java.util.Stack; + +class MinStack { + + Stack stack; + Stack minStack; + int min = Integer.MAX_VALUE; + + public MinStack() { + stack = new Stack<>(); + minStack = new Stack<>(); + } + + public void push(int val) { + min = Math.min(val, min); + stack.push(val); + minStack.push(min); + } + + public void pop() { + if(!stack.isEmpty()){ + stack.pop(); + } + if(!minStack.isEmpty()){ + minStack.pop(); + } + if(!minStack.isEmpty()){ + min = minStack.peek(); + }else{ + min = Integer.MAX_VALUE; + } + } + + public int top() { + return stack.peek(); + } + + public int getMin() { + return min; + } +} + +/** + * 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/MyHashSet.java b/MyHashSet.java new file mode 100644 index 00000000..34175cd4 --- /dev/null +++ b/MyHashSet.java @@ -0,0 +1,68 @@ +// Time Complexity : O(1) for all operations +// Space Complexity : O(n) where n is the number of elements in the hashset +// Did this code successfully run on Leetcode : Yes +// Approach: I am using 2D array where primary array is of size 1000 and secondary array is of size 1000. I am using 2 hash functions to calculate the index of the primary and secondary array. I am using boolean values to store the values in the hashset. If the value is present in the hashset, I am setting the value to true. If the value is removed from the hashset, I am setting the value to false. If the value is not present in the hashset, I am returning false. + +class MyHashSet { + + boolean[][] values; + int buckets; + int bucketItems; + + public MyHashSet() { + buckets = 1000; + bucketItems = 1000; + values = new boolean[buckets][]; + } + + private int hash1(int key){ + return key%buckets; + } + + private int hash2(int key){ + return key/bucketItems; + } + + public void add(int key) { + int index1 = hash1(key); + int index2 = hash2(key); + + if(values[index1] == null){ + if(index1 == 0){ + values[index1] = new boolean[bucketItems+1]; + }else{ + values[index1] = new boolean[bucketItems]; + } + } + values[index1][index2] = true; + } + + public void remove(int key) { + int index1 = hash1(key); + int index2 = hash2(key); + + if(values[index1] == null){ + return; + }else{ + values[index1][index2] = false; + } + } + + public boolean contains(int key) { + int index1 = hash1(key); + int index2 = hash2(key); + + if(values[index1] == null){ + return false; + } + return values[index1][index2]; + } +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.add(key); + * obj.remove(key); + * boolean param_3 = obj.contains(key); + */ \ No newline at end of file