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
57 changes: 57 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.Stack;

class MinStack {
/*
Initialize min to int max. When inserting value that is less than or equal to min,
we will first push the previous min value into the min stack and then push the current value.
Similarly, when popping value from stack, if the popped value is equal to the min value,
then we again pop an element and assign that as the min value.

// Time Complexity : push, pop, peek, getMin - O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
*/


private Stack<Integer> minSt;
int min;

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

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

public void pop() {
int val = minSt.pop();
if (val == min) {
min = minSt.pop();
}
}

public int top() {
return minSt.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();
*/
72 changes: 72 additions & 0 deletions MyHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class MyHashSet {

/*
Using 2d boolean array and double hashing method, when we want to add a key to the set,
we use the hash values to find the place in the array in O(1) time.
The first hash function gives the index for the main array and the second hash helps us identify
the place in the secondary array. To add a new value, we set the boolean to true at the calculated index.
To remove from the array, we set the value to false at the calculated index.

// Time Complexity : push, pop, peek, getMin - O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
*/

private boolean[][] storage;
private Integer bucket;
private Integer bucketItem;

public MyHashSet() {
this.bucket = 1000;
this.bucketItem = 1000;
this.storage = new boolean[bucket][];
}

private int hash1(int key) {
return key % this.bucket;
}

private int hash2(int key) {
return key / this.bucketItem;
}

public void add(int key) {
int bucket = hash1(key);
int bucketItem = hash2(key);
if (storage[bucket] == null) {
if (bucket == 0) {
storage[bucket] = new boolean[this.bucketItem + 1];
} else {
storage[bucket] = new boolean[this.bucketItem];
}
}
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);
*/