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
66 changes: 66 additions & 0 deletions DesignHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Time Complexity : O(1)
// Space Complexity : O(1) for user functions and O(n) for constructor
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach
/*
Maintain primary and secondary arrays by hashing with modulo and divide operations to store, remove
and check if it contains or not. We leverage primary and secondary hashes by computing the respective
key with that bucket size and operation.
*/
class MyHashSet {
boolean[][] arr;
int primaryBuckets;
int secondaryBuckets;

public MyHashSet() {
this.primaryBuckets = 1001;
this.secondaryBuckets = 1000;
this.arr = new boolean[primaryBuckets][];
}

private int getPrimaryHash(int key) {
return key / primaryBuckets;
}

private int getSecondaryHash(int key) {
return key % secondaryBuckets;
}

public void add(int key) {
int primaryHash = getPrimaryHash(key);
if(arr[primaryHash] == null) {
arr[primaryHash] = new boolean[secondaryBuckets];
}
int secondaryHash = getSecondaryHash(key);
arr[primaryHash][secondaryHash] = true;
}

public void remove(int key) {
int primaryHash = getPrimaryHash(key);
if(arr[primaryHash] == null) {
return;
}
int secondaryHash = getSecondaryHash(key);
arr[primaryHash][secondaryHash] = false;
}

public boolean contains(int key) {
int primaryHash = getPrimaryHash(key);
if(arr[primaryHash] == null) {
return false;
}
int secondaryHash = getSecondaryHash(key);
return arr[primaryHash][secondaryHash];
}
}

/**
* 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);
*/
53 changes: 53 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Time Complexity : O(1)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :yes


// Your code here along with comments explaining your approach
/*
Take a stack and also a min variable to keep track of minimum value. Whenever new input comes, we
first try to compare and check if its less than or equal to existing min value, if so, we store the
previous min in the stack and then the new input. Similarly, for pop, we make sure to check if we
are popping the min value, if so, we update the min variable with previous min value from stack.
*/
class MinStack {
Stack<Integer> st;
int min;

public MinStack() {
this.st = new Stack<>();
this.min = Integer.MAX_VALUE;
}

public void push(int val) {
if(val <= min) {
st.push(min);
min = val;
}
st.push(val);
}

public void pop() {
if(st.pop() == min)
min = st.pop();

}

public int top() {
return st.peek();
}

public int getMin() {
return min;
}
}

/**
* 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();
*/