diff --git a/HashSet.java b/HashSet.java new file mode 100644 index 000000000..5ef95c9e8 --- /dev/null +++ b/HashSet.java @@ -0,0 +1,75 @@ +// Time Complexity : O(1) for push, O(1) for min and O(1) for peek +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this :No + + +class MyHashSet { + + private boolean[][] storage; + private int buckets; + private int bucketItems; + + public MyHashSet() { + + this.buckets = 1000; + this.bucketItems = 1000; + this.storage = new boolean[buckets][]; + } + + private int hash1(int key) { + return key % this.buckets; + } + + private int hash2(int key) { + return key/this.buckets; + } + + public void add(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + + if(storage[bucket] == null) { + + if(bucket == 0){ + storage[bucket] = new boolean[bucketItems+1]; + } else { + storage[bucket] = new boolean[bucketItems]; + } + } + storage[bucket][bucketItem] = true; + } + + public void remove(int key) { + + int bucket = hash1(key); + int bucketItem = hash2(key); + + if(storage[bucket] == null) { + return; + } + + storage[bucket][bucketItem] = false; + } + + public boolean contains(int key) { + + int bucket = hash1(key); + int bucketSize = hash2(key); + + if(storage[bucket] == null) { + return false; + } + + return storage[bucket][bucketSize]; + + } +} + +/** + * 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/MinQueue.java b/MinQueue.java new file mode 100644 index 000000000..03fb2d4b3 --- /dev/null +++ b/MinQueue.java @@ -0,0 +1,39 @@ +// Time Complexity : O(1) for push, O(1) for min and O(1) for top +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this :No + +class MinStack { + + int min; + Stack stack; + Stack minStack; + + public MinStack() { + + this.min = Integer.MAX_VALUE; + stack = new Stack<>(); + minStack = new Stack<>(); + minStack.push(min); + } + + public void push(int val) { + stack.push(val); + min = Math.min(min,val); + minStack.push(min); + } + + public void pop() { + stack.pop(); + minStack.pop(); + min = minStack.peek(); + } + + public int top() { + return stack.peek(); + } + + public int getMin() { + return minStack.peek(); + } +} 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