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
53 changes: 53 additions & 0 deletions 155. Min Stack/MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class MinStack {

private Stack<Integer> st;
private Stack<Integer> minst;
private int min;

public MinStack() {

this.st = new Stack<>();
this.minst = new Stack<>();
this.min = Integer.MAX_VALUE;
minst.push(min); // setting minStack value to infinity
}

public void push(int val) {

if(val < min){
min = val;
}

st.push(val);
minst.push(min);

}

public void pop() {
st.pop();
minst.pop();
min = minst.peek(); // set min value to minst top value i.e. the min value
}

public int top() {

return st.peek();

}

public int getMin() {
return min;
}
}

// TC - O(1)
// SC - O(n)

/**
* 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();
*/
81 changes: 81 additions & 0 deletions 705. Design HashSet/MyHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class MyHashSet {

int primaryBucket;
int secondaryBucket;
boolean[][] storage;

public MyHashSet() {

this.primaryBucket = 1000;
this.secondaryBucket = 1000;
this.storage = new boolean[primaryBucket][];

}

public int primaryKey(int key) {

return key % primaryBucket;

}

public int secondaryKey(int key) {

return key / secondaryBucket;

}

public void add(int key) {

int primaryIndex = primaryKey(key);

if (storage[primaryIndex] == null) {

if (primaryIndex == 0) {
storage[primaryIndex] = new boolean[secondaryBucket + 1];
} else {
storage[primaryIndex] = new boolean[secondaryBucket];
}

}

int secondaryIndex = secondaryKey(key);
storage[primaryIndex][secondaryIndex] = true;

}

public void remove(int key) {

int primaryIndex = primaryKey(key);

if(storage[primaryIndex] == null){

return;
}

int secondaryIndex = secondaryKey(key);
storage[primaryIndex][secondaryIndex] = false;

}

public boolean contains(int key) {

int primaryIndex = primaryKey(key);

if (storage[primaryIndex] == null) {

return false;
}
int secondaryIndex = secondaryKey(key);

return storage[primaryIndex][secondaryIndex];

}
}

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