From 31ed82ebefae4b68ccc20ef78f9c43282aa1f1b1 Mon Sep 17 00:00:00 2001 From: sandeepkumarks Date: Sat, 6 Jun 2026 18:44:10 -0700 Subject: [PATCH] Added solution for HashSet and MinStack problems --- MinStack.java | 53 ++++++++++++++++++++++++++++++++++++++++++ MyHashSet.java | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 MinStack.java create mode 100644 MyHashSet.java diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 000000000..505764349 --- /dev/null +++ b/MinStack.java @@ -0,0 +1,53 @@ +// Time Complexity : O(1) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Description: +// - Store all values in the main stack. +// - Store the minimum value seen so far at each corresponding position in a second stack. +// - When popping, the top of the min stack automatically reveals the minimum for the remaining elements. + +class MinStack { + private final Stack stack1; + private final Stack stack2; + private int min; + + public MinStack() { + this.stack1 = new Stack<>(); + this.stack2 = new Stack<>(); + this.min = Integer.MAX_VALUE; + } + + public void push(int value) { + stack1.push(value); + this.min = Math.min(min, value); + stack2.push(this.min); + } + + public void pop() { + if(stack1.isEmpty()) { + return; + } + + stack1.pop(); + stack2.pop(); + if(stack2.isEmpty()) { + this.min = Integer.MAX_VALUE; + } else { + this.min = stack2.peek(); + } + } + + public int top() { + if(stack1.isEmpty()) { + return -1; + } + + return stack1.peek(); + } + + public int getMin() { + return this.min; + } +} \ No newline at end of file diff --git a/MyHashSet.java b/MyHashSet.java new file mode 100644 index 000000000..d72291c68 --- /dev/null +++ b/MyHashSet.java @@ -0,0 +1,62 @@ +// Time Complexity : O(1) +// Space Complexity : O(k), where k is the max key range. ie: O(10^6) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Description: +// - Use a 2D boolean array where the first hash selects a bucket and the second hash selects an index within that bucket. +// - Allocate sub-buckets lazily to avoid creating space for all possible keys upfront. + +class MyHashSet { + + private static final int BUCKET_SIZE = 1000; + private static final int SUB_BUCKET_SIZE = 1000; + + private final boolean[][] buckets; + + public MyHashSet() { + this.buckets = new boolean[this.BUCKET_SIZE][]; + } + + public void add(int key) { + int hash1 = hash1(key); + if(buckets[hash1] == null) { + int subBucketSize = this.SUB_BUCKET_SIZE; + if(hash1 == 0) { + subBucketSize += 1; + } + buckets[hash1] = new boolean[subBucketSize]; + } + int hash2 = hash2(key); + buckets[hash1][hash2] = true; + } + + public void remove(int key) { + int hash1 = hash1(key); + if(buckets[hash1] == null) { + return; + } + int hash2 = hash2(key); + + buckets[hash1][hash2] = false; + } + + public boolean contains(int key) { + int hash1 = hash1(key); + if(buckets[hash1] == null) { + return false; + } + + int hash2 = hash2(key); + + return buckets[hash1][hash2]; + } + + private int hash1(int key) { + return key % this.BUCKET_SIZE; + } + + private int hash2(int key) { + return key / this.BUCKET_SIZE; + } +} \ No newline at end of file