diff --git a/HashSet.java b/HashSet.java new file mode 100644 index 00000000..05abe1da --- /dev/null +++ b/HashSet.java @@ -0,0 +1,60 @@ + +// Time Complexity : O(1) for add (amortized), remove and contains +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : + +// this approach uses double hashing to store the keys in a 2D boolean array. +// The primary hash function determines the index of the first dimension, +// while the secondary hash function determines the index of the second dimension. +// This allows us to efficiently store and retrieve keys while minimizing space usage. + +class MyHashSet { + int primaryBuckets; + int secondaryBuckets; + boolean[][] storage; + + public MyHashSet() { + primaryBuckets = 1000; + secondaryBuckets = 1000; + storage = new boolean[primaryBuckets][]; + } + + int getPrimaryHash(int key){ + return key%primaryBuckets; + } + int getSecondaryHash(int key){ + return key/secondaryBuckets; + } + + public void add(int key) { + int primaryIndex = getPrimaryHash(key); + if(storage[primaryIndex]==null){ + if(primaryIndex == 0){ + storage[primaryIndex] = new boolean[secondaryBuckets +1]; + }else{ + storage[primaryIndex] = new boolean[secondaryBuckets]; + } + } + int secondaryIndex = getSecondaryHash(key); + storage[primaryIndex][secondaryIndex] = true; + } + + public void remove(int key) { + int primaryIndex = getPrimaryHash(key); + if(storage[primaryIndex]==null){ + return; + } + int secondaryIndex = getSecondaryHash(key); + storage[primaryIndex][secondaryIndex] = false; + } + + public boolean contains(int key) { + int primaryIndex = getPrimaryHash(key); + if(storage[primaryIndex]==null){ + return false; + } + int secondaryIndex = getSecondaryHash(key); + return storage[primaryIndex][secondaryIndex]; + } +} \ No newline at end of file diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 00000000..c78f10d1 --- /dev/null +++ b/MinStack.java @@ -0,0 +1,47 @@ +// Time Complexity : O(1) for push, pop, top and getMin +// Space Complexity : O(n) where n is the number of elements in the stack +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : + +// This approach uses two stacks: +// a primary stack to store all the elements and +// a min stack to keep track of the minimum element at each level of the primary stack. +// Whenever a new element is pushed onto the primary stack, +// we compare it with the current minimum element (the top of the min stack) and +// push the smaller of the two onto the min stack. + +import java.util.Stack; + +class MinStack { + Stack primaryStack; + Stack minStack; + public MinStack() { + primaryStack = new Stack(); + minStack = new Stack(); + } + + public void push(int val) { + primaryStack.push(val); + if(minStack.isEmpty() || val < minStack.peek()){ + minStack.push(val); + } else { + minStack.push(minStack.peek()); + } + } + + public void pop() { + if(primaryStack.isEmpty()){ + return; + } + primaryStack.pop(); + minStack.pop(); + } + + public int top() { + return primaryStack.peek(); + } + + public int getMin() { + return minStack.peek(); + } +} 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