From 65efa0a32a2cfcae2b6d0cba962272b5e0210d65 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Tue, 7 Apr 2026 13:47:36 -0700 Subject: [PATCH 1/5] Design 1 --- Sample.java | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/Sample.java b/Sample.java index 1739a9cb..63fac1e9 100644 --- a/Sample.java +++ b/Sample.java @@ -1,7 +1,85 @@ // Time Complexity : // Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : I took the help of a +// video to understand the concept of hash map and how to implement +// it using separate chaining approach. +// Your code here along with comments explaining your approach +// Seperate chaining approach to handle collisions in hash map +// Linked List is used to store the key value pairs +// Rehashing is done when the load factor exceeds 0.75 +import java.util.LinkedList; -// Your code here along with comments explaining your approach +class Main { + public static void main(String args[]) + { + MyHashSet obj = new MyHashSet(); + obj.add(1); + obj.add(2); + System.out.println(obj.contains(1)); // returns true + System.out.println(obj.contains(3)); // returns false (not found) + obj.add(2); + System.out.println(obj.contains(2)); // returns true + obj.remove(2); + System.out.println(obj.contains(2)); // returns false (already removed) + } +} + +class MyHashSet { + class Entry{ + public int key; + public Entry(int key){ + this.key = key; + } + } + LinkedList[] set; + public static int size = 769; + public MyHashSet() { + set = new LinkedList[size]; + } + + public void add(int key) { + int bucket = (key % size); + if (set[bucket] == null) set[bucket] = new LinkedList<>(); + for (Entry e : set[bucket]) { + if (e.key == key) return; + } + set[bucket].addLast(new Entry(key)); + } + + public void remove(int key) { + int bucket = (key % size); + if (set[bucket] != null) { + Entry toRemove=null; + for (Entry e : set[bucket]) { + if (e.key == key) + { + toRemove=e; + break; + } + } + if(toRemove!=null) set[bucket].remove(toRemove); + } + + + } + + public boolean contains(int key) { + int bucket = (key % size + size) % size; + if (set[bucket] != null) { + for (Entry e : set[bucket]) { + if (e.key == key) return true; + } + } + return false; + } +} + +/** + * 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 From bd9b92f45ca37dc7e44c20ca80e5f6abac3dca6b Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Tue, 7 Apr 2026 13:56:30 -0700 Subject: [PATCH 2/5] Design 1 --- Sample.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sample.java b/Sample.java index 63fac1e9..4f583355 100644 --- a/Sample.java +++ b/Sample.java @@ -1,5 +1,5 @@ -// Time Complexity : -// Space Complexity : +// Time Complexity :O(1) average, O(n) worst case +// Space Complexity :O(n) // Did this code successfully run on Leetcode : Yes // Any problem you faced while coding this : I took the help of a // video to understand the concept of hash map and how to implement From 5fbef5233c616866d8616b30795eec53c4b29ee6 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Tue, 7 Apr 2026 15:50:15 -0700 Subject: [PATCH 3/5] Design 1 Min Stack --- MinStack.java | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 MinStack.java diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 00000000..ef70213d --- /dev/null +++ b/MinStack.java @@ -0,0 +1,88 @@ +// Time Complexity : O(1) for push, pop, top, and getMin +// Space Complexity : O(n) as we store all elements in the stack +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Faced minor difficulty in translating tuple-based approach to Java, I had done in c# before. +// Your code here along with comments explaining your approach +// As a pair we can store the value and the minimum value at that point in the stack. +// So when we push a new value, we can compare it with the current minimum and store the new minimum if necessary. +// This way, we can retrieve the minimum value in O(1) time by looking at the top of the stack. +import java.util.Stack; +class MinStack { + + class Pair { + long val; + long min; + + Pair(long val, long min) { + this.val = val; + this.min = min; + } + } + + Stack st; + + public MinStack() { + st = new Stack<>(); + } + public void push(int val) { + if(st.isEmpty()) + { + st.push(new Pair(val,val)); + } + else + { + long currMin=Math.min(val,st.peek().min); + st.push(new Pair(val,currMin)); + } + } + + public void pop() { + if (!st.isEmpty()) { + st.pop(); + } + } + + public int top() { + if (!st.isEmpty()) { + return (int) st.peek().val; + } + throw new RuntimeException("Stack is empty."); + } + + public int getMin() { + if (!st.isEmpty()) { + return (int) st.peek().min; + } + throw new RuntimeException("Stack is empty."); + } +} + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.push(val); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ +class Main { + public static void main(String args[]) + { + MinStack obj = new MinStack(); + obj.push(-2); + obj.push(0); + obj.push(-3); + System.out.println(obj.getMin()); // return -3 + obj.pop(); + System.out.println(obj.top()); // return 0 + System.out.println(obj.getMin()); // return -2 + } +} +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.push(val); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ \ No newline at end of file From d6eddbed19b0895872e378cb79892063035d24d2 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Tue, 7 Apr 2026 15:52:33 -0700 Subject: [PATCH 4/5] Design 1 HashSet --- Sample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sample.java b/Sample.java index 4f583355..b48b020c 100644 --- a/Sample.java +++ b/Sample.java @@ -66,7 +66,7 @@ public void remove(int key) { } public boolean contains(int key) { - int bucket = (key % size + size) % size; + int bucket = (key % size); if (set[bucket] != null) { for (Entry e : set[bucket]) { if (e.key == key) return true; From d05e6efc2d7ac18805e817add6c59b5b3d1334db Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Tue, 7 Apr 2026 15:54:41 -0700 Subject: [PATCH 5/5] Design 1 HashSet and Min Stack --- MinStack.java | 28 +++++++++++++++++++--------- Sample.java | 15 +-------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/MinStack.java b/MinStack.java index ef70213d..429b927f 100644 --- a/MinStack.java +++ b/MinStack.java @@ -57,6 +57,15 @@ public int getMin() { } } +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.push(val); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ + /** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); @@ -76,13 +85,14 @@ public static void main(String args[]) obj.pop(); System.out.println(obj.top()); // return 0 System.out.println(obj.getMin()); // return -2 + MyHashSet obj1 = new MyHashSet(); + obj1.add(1); + obj1.add(2); + System.out.println(obj1.contains(1)); // returns true + System.out.println(obj1.contains(3)); // returns false (not found) + obj1.add(2); + System.out.println(obj1.contains(2)); // returns true + obj1.remove(2); + System.out.println(obj1.contains(2)); // returns false (already removed) } -} -/** - * Your MinStack object will be instantiated and called as such: - * MinStack obj = new MinStack(); - * obj.push(val); - * obj.pop(); - * int param_3 = obj.top(); - * int param_4 = obj.getMin(); - */ \ No newline at end of file +} \ No newline at end of file diff --git a/Sample.java b/Sample.java index b48b020c..f4fbb4ba 100644 --- a/Sample.java +++ b/Sample.java @@ -11,20 +11,7 @@ import java.util.LinkedList; -class Main { - public static void main(String args[]) - { - MyHashSet obj = new MyHashSet(); - obj.add(1); - obj.add(2); - System.out.println(obj.contains(1)); // returns true - System.out.println(obj.contains(3)); // returns false (not found) - obj.add(2); - System.out.println(obj.contains(2)); // returns true - obj.remove(2); - System.out.println(obj.contains(2)); // returns false (already removed) - } -} + class MyHashSet { class Entry{