From 3e8897735b1ec27cdfaceb26d97286a20e672d1b Mon Sep 17 00:00:00 2001 From: Suresh Date: Tue, 7 Apr 2026 21:30:36 +0530 Subject: [PATCH 1/4] Done Design-1 --- Sample.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Sample.java b/Sample.java index 1739a9cb..9e0fdb2a 100644 --- a/Sample.java +++ b/Sample.java @@ -5,3 +5,37 @@ // Your code here along with comments explaining your approach +import java.util.Stack; + +class MinStack { + Stack mainStack; + Stack minStack; + + public MinStack() { + 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 From 1c7ae9582060b8708dc9aed84c5d299ba647834e Mon Sep 17 00:00:00 2001 From: pavanbollam Date: Tue, 7 Apr 2026 21:42:17 +0530 Subject: [PATCH 2/4] Code cleanup --- Sample.java | 1 - 1 file changed, 1 deletion(-) diff --git a/Sample.java b/Sample.java index 9e0fdb2a..83ee408f 100644 --- a/Sample.java +++ b/Sample.java @@ -37,5 +37,4 @@ public int getMin() { } } - \ No newline at end of file From 3f575803f3780565bdaca069e770b03767b1ea8d Mon Sep 17 00:00:00 2001 From: pavanbollam Date: Tue, 7 Apr 2026 21:46:09 +0530 Subject: [PATCH 3/4] code cleanup --- Sample.java | 1 - 1 file changed, 1 deletion(-) diff --git a/Sample.java b/Sample.java index 83ee408f..a9fad47a 100644 --- a/Sample.java +++ b/Sample.java @@ -3,7 +3,6 @@ // Did this code successfully run on Leetcode : // Any problem you faced while coding this : - // Your code here along with comments explaining your approach import java.util.Stack; From 33ab612be274c24d3982c092ef19043c2f84687a Mon Sep 17 00:00:00 2001 From: pavankumarbollam Date: Sat, 25 Apr 2026 17:36:07 +0530 Subject: [PATCH 4/4] Done Design-1 --- CustomHashSet.java | 82 ++++++++++++++++++++++ Sample.java => MinStackUsingTwoStacks.java | 15 ++-- 2 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 CustomHashSet.java rename Sample.java => MinStackUsingTwoStacks.java (55%) 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/Sample.java b/MinStackUsingTwoStacks.java similarity index 55% rename from Sample.java rename to MinStackUsingTwoStacks.java index a9fad47a..cf8f6ed3 100644 --- a/Sample.java +++ b/MinStackUsingTwoStacks.java @@ -1,16 +1,19 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : +// 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 MinStack { +class MinStackUsingTwoStacks { Stack mainStack; Stack minStack; - public MinStack() { + public MinStackUsingTwoStacks() { mainStack = new Stack<>(); minStack = new Stack<>(); }