diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 00000000..4c8bcb1f --- /dev/null +++ b/MinStack.java @@ -0,0 +1,36 @@ +import java.util.Stack; + +class MinStack { + + int min; + Stack st; + public MinStack() { + st = new Stack<>(); + min = Integer.MAX_VALUE; + st.push(min); + } + + public void push(int val) { + if(val <= min){ + st.push(min); + min = val; + } + st.push(val); + } + + public void pop() { + int valPopped = st.pop(); + if(valPopped == min){ + min = st.pop(); + } + } + + public int top() { + + return st.peek(); + } + + public int getMin() { + return min; + } +} diff --git a/MyHashSet.java b/MyHashSet.java new file mode 100644 index 00000000..aadaf35c --- /dev/null +++ b/MyHashSet.java @@ -0,0 +1,68 @@ +// 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 +class MyHashSet { + + private boolean storage[][]; + private int arraySize1; + private int arraySize2; + + + public MyHashSet() { + + this.arraySize1 = 1000; + this.arraySize2 = 1000; + this.storage = new boolean[arraySize1][]; + + } + + private int hash1(int key){ + + return key%arraySize1; + + } + + private int hash2(int key){ + return key/arraySize2; + } + + public void add(int key) { + + int arrayIndex1 = hash1(key); + int arrayIndex2 = hash2(key); + + if(storage[arrayIndex1] == null){ + if(arrayIndex1 == 0){ + storage[arrayIndex1]= new boolean[arraySize2+1]; + }else{ + storage[arrayIndex1]= new boolean[arraySize2]; + } + } + + storage[arrayIndex1][arrayIndex2] = true; + } + + public void remove(int key) { + + int arrayIndex1 = hash1(key); + int arrayIndex2 = hash2(key); + + if( storage[arrayIndex1] == null) return; + storage[arrayIndex1][arrayIndex2] = false; + } + + public boolean contains(int key) { + + int arrayIndex1 = hash1(key); + int arrayIndex2 = hash2(key); + + if(storage[arrayIndex1] == null) return false; + + return storage[arrayIndex1][arrayIndex2]; + + } +} 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