diff --git a/CustomHashSet.java b/CustomHashSet.java new file mode 100644 index 00000000..0e6ee581 --- /dev/null +++ b/CustomHashSet.java @@ -0,0 +1,82 @@ +// Time Complexity : O(N) +// Space Complexity : O(M + N) Where M is your fixed array of 1,000 buckets, N is the actual number of nodes created to store data +// 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 +// The CustomHashSet uses an array of 1,000 buckets, where each bucket acts as a Linked List to store keys that share the same hash index. +// The add, remove, and contains methods calculate the index using the modulo operator and then traverse the corresponding list to perform the operation. +// By using a dummy node at the start of every bucket, the code simplifies deletion logic and ensures unique keys by checking for duplicates before every insertion. + +public class CustomHashSet { + class Node { + int key; + Node next; + + public Node(int key) { + this.key = key; + this.next = null; + } + } + public static final int size = 1000; + public Node[] buckets; + + + public CustomHashSet() { + buckets = new Node[size]; + } + + public int getHash(int key) { + return key % size; + } + + public void add(int key) { + int index = getHash(key); + + if (buckets[index] == null) { + buckets[index] = new Node(-1); + } + Node previousNode = this.buckets[index]; + Node currentNode = previousNode.next; + + while (currentNode != null) { + if (key == currentNode.key) { + currentNode.key = key; + return; + } + previousNode = currentNode; + currentNode = currentNode.next; + } + Node newNode = new Node(key); + previousNode.next = newNode; + + } + + public void remove(int key) { + int index = getHash(key); + if (buckets[index] == null) return; + Node previousNode = buckets[index]; + Node currentNode = previousNode.next; + + while(currentNode != null) { + if (key == currentNode.key) { + previousNode.next = currentNode.next; + } + previousNode = currentNode; + currentNode = currentNode.next; + } + } + + public boolean contains(int key) { + int index = getHash(key); + if (buckets[index] == null) return false; + Node currentNode = buckets[index].next; + while(currentNode != null) { + if (key == currentNode.key) { + return true; + } + currentNode = currentNode.next; + } + return false; + } +} diff --git a/MinStackUsingTwoStacks.java b/MinStackUsingTwoStacks.java new file mode 100644 index 00000000..cf8f6ed3 --- /dev/null +++ b/MinStackUsingTwoStacks.java @@ -0,0 +1,42 @@ +// Time Complexity : O(1) +// Space Complexity : O(N) +// 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 +// The mainStack stores all values normally, while the minStack tracks the minimum element by only pushing values that are less than or equal to its current top. +// When you pop, if the removed value is the current minimum, it is popped from the minStack as well to keep the minimums synced with the remaining data. + +import java.util.Stack; + +class MinStackUsingTwoStacks { + Stack mainStack; + Stack minStack; + + public MinStackUsingTwoStacks() { + mainStack = new Stack<>(); + minStack = new Stack<>(); + } + + public void push(int val) { + mainStack.push(val); + if (minStack.isEmpty() || val <= minStack.peek()) + minStack.push(val); + } + + public void pop() { + int poppedValue = mainStack.pop(); + if (poppedValue == minStack.peek()) + minStack.pop(); + } + + public int top() { + return mainStack.peek(); + } + + public int getMin() { + return minStack.peek(); + } +} + + \ No newline at end of file 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