diff --git a/Design HashSet.java b/Design HashSet.java new file mode 100644 index 00000000..bce23d17 --- /dev/null +++ b/Design HashSet.java @@ -0,0 +1,50 @@ +//Approach : +//TC : O(1) +//SC : O(1) +class MyHashSet { + int primaryKeyLen = 1000; + int secondaryKeyLen = 1000; + boolean[][] set; + + public MyHashSet() { + set = new boolean[1000][]; //SC: 10^3 = constant + } + + public void add(int key) { //TC: O(1) // SC: 10^3= constant = O(1) + int z = key % primaryKeyLen; + if (set[z] == null) { + set[z] = new boolean[1000]; //SC: 10^3 = constant + } + int s = key / 1000; + set[z][s] = true; + } + + public void remove(int key) { //TC: O(1), SC: O(1) + int z = key % 1000; + int s = key / 1000; + if (set[z] == null) { + return; + } + set[z][s] = false; + } + + public boolean contains(int key) {//TC: O(1), SC:O(1) + int z = key % 1000; + int s = key / 1000; + if (set[z] == null) { + return false; + } + if (set[z][s] == true) { + return true; + } + return false; + } +} + +/** + * 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 diff --git a/minStack.java b/minStack.java new file mode 100644 index 00000000..a95f27c5 --- /dev/null +++ b/minStack.java @@ -0,0 +1,52 @@ + +import java.util.*; +//Approach: +//TC: O(1) +//SC: 2*O(N) + +class MinStack { + + Stack st; + Stack minSt; + + public MinStack() { + st = new Stack(); + minSt = new Stack(); + minSt.push(Integer.MAX_VALUE); + } + + public void push(int value) { + + if (minSt.peek() >= value) { + minSt.push(value); + + } + st.push(value); + } + + public void pop() { + + if (st.peek().equals(minSt.peek())) { + minSt.pop(); + } + st.pop(); + + } + + public int top() { + return st.peek(); + } + + public int getMin() { + return minSt.peek(); + } +} + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.push(value); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ \ No newline at end of file