From ccde14b1ac2934123b966ac43062c43d41f7e9df Mon Sep 17 00:00:00 2001 From: Manisha Rana Date: Sun, 21 Dec 2025 16:19:30 -0500 Subject: [PATCH 1/2] Design-1 Done --- HashSet.java | 30 +++++++++++++++++++++++++++++ MinStack.java | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 HashSet.java create mode 100644 MinStack.java diff --git a/HashSet.java b/HashSet.java new file mode 100644 index 00000000..83ae288c --- /dev/null +++ b/HashSet.java @@ -0,0 +1,30 @@ +// Time Complexity : O(1) +// Space Complexity : O(1), no additional space used +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +import java.util.HashMap; +import java.util.Map; + +public class HashSet { + // Keep hashmap of value and state indicating if it is present in the set + Map set = new HashMap<>(); + + HashSet() {} + + public void add(Integer value) { + if (!contains(value)) { + set.put(value, true); + } + } + + public void remove(Integer value) { + if (!contains(value)) { + set.remove(value); + } + } + + public boolean contains(Integer value) { + return set.containsKey(value); + } + +} diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 00000000..4fcae948 --- /dev/null +++ b/MinStack.java @@ -0,0 +1,52 @@ +// Time Complexity : O(1) for all operations +// Space Complexity : O(N) in worst case, if inputs are sorted in descending order. The minstack will hold N values. +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +import java.util.Stack; + +public class MinStack { + //Keep 2 stacks, one to keep all values + Stack stack = new Stack<>(); + //In other one, push new value if it smaller than the current top value in the stack + Stack minValues = new Stack<>(); + + MinStack() { + } + + void push(int val) { + stack.push(val); + if (minValues.isEmpty() || minValues.peek() >= val) { + minValues.push(val); + } + } + + void pop() { + if (stack.isEmpty()) { + System.out.println("Stack is Empty"); + } + + if (stack.peek().equals(minValues.peek())) { + stack.pop(); + minValues.pop(); + } else { + stack.pop(); + } + } + + int top() { + if (stack.isEmpty()) { + System.out.println("Stack is Empty"); + return -1; + } + return stack.peek(); + } + + int getMin() { + if (stack.isEmpty() || minValues.isEmpty()) { + System.out.println("Stack is Empty"); + return -1; + } + return minValues.peek(); + } +} From 00d8c9a90183deb6267ee59638cbd60d70f6e38e Mon Sep 17 00:00:00 2001 From: Manisha Rana Date: Tue, 23 Dec 2025 15:19:20 -0500 Subject: [PATCH 2/2] adding version 2 of hashset --- HashSet.java | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/HashSet.java b/HashSet.java index 83ae288c..148f728f 100644 --- a/HashSet.java +++ b/HashSet.java @@ -1,30 +1,48 @@ -// Time Complexity : O(1) +// Time Complexity : O(1) average case // Space Complexity : O(1), no additional space used // Did this code successfully run on Leetcode : Yes // Any problem you faced while coding this : No -import java.util.HashMap; -import java.util.Map; + +import java.util.ArrayList; +import java.util.List; public class HashSet { - // Keep hashmap of value and state indicating if it is present in the set - Map set = new HashMap<>(); + private static final int MAX_SIZE = 1000; + private final List[] set = new List[MAX_SIZE]; HashSet() {} public void add(Integer value) { - if (!contains(value)) { - set.put(value, true); + if (contains(value)) { + return; + } + int position = hashFunction(value); + List valuesAtPosition = set[position]; + if (valuesAtPosition == null) { + set[position] = new ArrayList<>(); } + set[position].add(value); } public void remove(Integer value) { - if (!contains(value)) { - set.remove(value); + if (contains(value)) { + int position = hashFunction(value); + List valuesAtPosition = set[position]; + valuesAtPosition.remove(value); } } public boolean contains(Integer value) { - return set.containsKey(value); + int position = hashFunction(value); + if (set[position] == null || set[position].isEmpty()) return false; + for (Integer currentValue : set[position]) { + if (currentValue.equals(value)) return true; + } + return false; + } + + private int hashFunction(Integer value) { + return value % MAX_SIZE; } }