Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -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);
*/
45 changes: 45 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -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<Integer> st; // main stack
Stack<Integer> 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();
}
}