diff --git a/HashSet.java b/HashSet.java new file mode 100644 index 000000000..e9c08ddc8 --- /dev/null +++ b/HashSet.java @@ -0,0 +1,99 @@ +// Time Complexity : add, remove, contains = O(1) +// Space Complexity : O(n * m) where n = buckets, m = bucketItems +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach +class MyHashSet { + + //If element is present then it is true else false + private boolean[][] storage; + //size of array + private int buckets; + //size of nested array + private int bucketItems; + + //Hash 1 + public int bucket(int key) { + //we get location in array + return key % buckets; + } + + //Hash 2 :- input will never have same hash1 and hash2 hence no collision + public int bucketItem(int key) { + //we get location in nested array + return key / bucketItems; + } + + public MyHashSet() { + + //we take square root of 10^6 which is range of inputs + this.buckets = 1000; + this.bucketItems = 1000; + + //We do not give nested array size because if there is no element in that index we will not create the nested array thereby saving space + storage = new boolean[buckets][]; + } + + public void add(int key) { + + int location = bucket(key); + + //if the array location is empty we will create nested array and add the element + if(storage[location] == null) { + + if(location == 0) { + + //We do +1 here because for eg:- if the input is 10^6. The size of array is 1000 which is 0 to 999 and nested array is also 0-999. but if input is 10^6, we wont be able to store the value in nested array as 10^6 / 1000 = 1000. + storage[location] = new boolean[bucketItems + 1]; + + }else { + storage[location] = new boolean[bucketItems]; + } + } + + //location in the nested array + int nesLocation = bucketItem(key); + storage[location][nesLocation] = true; + } + + public void remove(int key) { + + int location = bucket(key); + + //It means this element is not present in hash set + if(storage[location] == null) + return; + + //If it is present we will find the exact location in nested array + int nesLocation = bucketItem(key); + + //If there was an element in the index it would be true + storage[location][nesLocation] = false; + + } + + public boolean contains(int key) { + + int location = bucket(key); + + //It means this element is not present in hash set + if(storage[location] == null) + return false; + + //Find the exact location of key + int nesLocation = bucketItem(key); + + //If there is an element it is true else false + return storage[location][nesLocation]; + } +} + +/** + * 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); + */ diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 000000000..42b6085b7 --- /dev/null +++ b/MinStack.java @@ -0,0 +1,65 @@ +// Time Complexity : Push = O(1), Pop = O(1), top = O(1), getMin = O(1) +// Space Complexity : O(n) where n = number of elements in the stack +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach +class MinStack { + + int min; + Stack stack; + Stack minStack; + + public MinStack() { + + this.min = Integer.MAX_VALUE; + stack = new Stack<>(); + minStack = new Stack<>(); + } + + public void push(int val) { + + //If we get a new value which is smaller than current min + //We will save it as current min. But we also save previous min because we need it if we pop current min in future + //Even if new val == min we save it + if(min >= val) { + minStack.push(min); + min = val; + } + + stack.push(val); + } + + public void pop() { + + int popped = stack.pop(); + + //If we are popping the minimum value then our minimum will be previous min + if(popped == min) { + //Store prev min in min + min = minStack.pop(); + } + } + + public int top() { + + //get the top element from stack + return stack.peek(); + } + + public int getMin() { + + //we store current minimum in min + 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/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cbc..000000000 --- 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