From 020a0bb106ccd1df57c4119cdd2356ec6e726186 Mon Sep 17 00:00:00 2001 From: gurneetk186 Date: Mon, 24 Nov 2025 09:35:45 -0800 Subject: [PATCH] Completing Design-1 --- Problem1.java | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ Problem2.java | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 Problem1.java create mode 100644 Problem2.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..a688d9c6 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,60 @@ +// Time Complexity : O(1) average for add, remove, contains +// Space Complexity : O(N) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No major issues; mostly indexing logic. + +// I used a 2D boolean array to simulate hashing with double hashing. +// The first hash maps to a bucket, and the second hash maps to the bucket index. +// When a bucket is needed, it is lazily initialized to save memory. +class MyHashSet { +private int bucket; +private int bucketItems; +private boolean [][]storage; + + public MyHashSet() { + this.bucket=1000; + this.bucketItems=1000; + this.storage = new boolean [bucket][]; + + } + private int hash1(int key){ + return key % this.bucket; + } + private int hash2(int key){ + return key/1000; + + } + public void add(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + if (storage [bucket]== null){ + storage [bucket] = new boolean [bucketItems]; + } + storage[bucket][bucketItem] = true; + + } + + public void remove(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + if (storage [bucket]== null)return ; + storage[bucket][bucketItem]=false; + + } + + public boolean contains(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + if (storage[bucket] == null)return false; + return storage[bucket][bucketItem]; + + } +} + +/** + * 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); + */ diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..d853817a --- /dev/null +++ b/Problem2.java @@ -0,0 +1,45 @@ +// Time Complexity : O(1) for push, pop, top, and getMin +// Space Complexity : O(n) due to the auxiliary min stack +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No major issues; logic of syncing the two stacks was important. + +// I used two stacks — one to store values and another to track the minimum values. +// Every time a new value is pushed, it is also pushed to the min stack if it is smaller or equal to the current minimum. +// When popping, if the popped value equals the min stack's top, I pop from the min stack as well to maintain correct minimum tracking. +class MinStack { + + Stack st; // main stack + Stack minStack; // keeps track of minimum values + + public MinStack() { + st = new Stack<>(); + minStack = new Stack<>(); + } + + public void push(int val) { + st.push(val); + + // If minStack is empty OR new value is <= current min, push it + if (minStack.isEmpty() || val <= minStack.peek()) { + minStack.push(val); + } + } + + public void pop() { + int removed = st.pop(); + + // If removed element is the current minimum, pop from minStack also + if (removed == minStack.peek()) { + minStack.pop(); + } + } + + public int top() { + return st.peek(); + } + + public int getMin() { + return minStack.peek(); + } +} +