diff --git a/155. Min Stack/MinStack.java b/155. Min Stack/MinStack.java new file mode 100644 index 00000000..85e71169 --- /dev/null +++ b/155. Min Stack/MinStack.java @@ -0,0 +1,53 @@ +class MinStack { + + private Stack st; + private Stack minst; + private int min; + + public MinStack() { + + this.st = new Stack<>(); + this.minst = new Stack<>(); + this.min = Integer.MAX_VALUE; + minst.push(min); // setting minStack value to infinity + } + + public void push(int val) { + + if(val < min){ + min = val; + } + + st.push(val); + minst.push(min); + + } + + public void pop() { + st.pop(); + minst.pop(); + min = minst.peek(); // set min value to minst top value i.e. the min value + } + + public int top() { + + return st.peek(); + + } + + public int getMin() { + return min; + } +} + +// TC - O(1) +// SC - O(n) + +/** + * 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/705. Design HashSet/MyHashSet.java b/705. Design HashSet/MyHashSet.java new file mode 100644 index 00000000..8757427b --- /dev/null +++ b/705. Design HashSet/MyHashSet.java @@ -0,0 +1,81 @@ +class MyHashSet { + + int primaryBucket; + int secondaryBucket; + boolean[][] storage; + + public MyHashSet() { + + this.primaryBucket = 1000; + this.secondaryBucket = 1000; + this.storage = new boolean[primaryBucket][]; + + } + + public int primaryKey(int key) { + + return key % primaryBucket; + + } + + public int secondaryKey(int key) { + + return key / secondaryBucket; + + } + + public void add(int key) { + + int primaryIndex = primaryKey(key); + + if (storage[primaryIndex] == null) { + + if (primaryIndex == 0) { + storage[primaryIndex] = new boolean[secondaryBucket + 1]; + } else { + storage[primaryIndex] = new boolean[secondaryBucket]; + } + + } + + int secondaryIndex = secondaryKey(key); + storage[primaryIndex][secondaryIndex] = true; + + } + + public void remove(int key) { + + int primaryIndex = primaryKey(key); + + if(storage[primaryIndex] == null){ + + return; + } + + int secondaryIndex = secondaryKey(key); + storage[primaryIndex][secondaryIndex] = false; + + } + + public boolean contains(int key) { + + int primaryIndex = primaryKey(key); + + if (storage[primaryIndex] == null) { + + return false; + } + int secondaryIndex = secondaryKey(key); + + return storage[primaryIndex][secondaryIndex]; + + } +} + +/** + * 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