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
77 changes: 77 additions & 0 deletions HashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//s30: https://www.thes30.com/problem/5c91d568fdc132001682c131/solutions
//https://www.youtube.com/watch?v=G1biLTsvrWE
// Used Two dimensional array with 1000 rows and 1000 cols. In total,we can store 10^6 elements as given in input.
//Identifying row using Hash1 method.
//Identifying col using Hash2 method.
//Add- place an element at that place. //Remove- removing an element from that place. //Check- checking an element from the respective place.
//TC: O(1)
//SC: O(1)
class MyHashSet {
int primaryBuckets;
int secondaryBuckets;
boolean[][] storage;

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

}
private int hash1(int key){
return key%1000;
}

private int hash2(int key){
return key/1000;
}

public void add(int key) {
int h1=hash1(key);
//To handle edge case.. 0 to 1000000 contains 1000001 elements. So, add extra size for one array to hold that extra element
if(storage[h1]==null){
if(h1==0){
storage[h1]=new boolean[secondaryBuckets+1];
}else{
storage[h1]=new boolean[secondaryBuckets];
}

}

int h2=hash2(key);
storage[h1][h2]=true;
}

//TC: O(1)
//SC: O(1)
public void remove(int key) {
int h1=hash1(key);
if(storage[h1]!=null){
int h2=hash2(key);
storage[h1][h2]=false;
}

}
//TC: O(1)
//SC: O(1)
public boolean contains(int key) {
int h1=hash1(key);
if(storage[h1]!=null){
int h2=hash2(key);
if(storage[h1][h2]==false){
return false;
}else{
return true;
}
}else{
return false;
}
}
}

/**
* 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 @@
//Using two stacks: MainStack and MinStack
//Maintaing min variable.
//For every addition, pushing that value to mainStack. Caluclating minimum. And pushing that min to minStack which means min element for all the elements which present in Stack.
//While removing, removing from both the stacks and peeking the top element to min variable.
//getMin: returing min variable
//top: Peeking top element of mainStack.

//TC: O(1); SC:O(1)

class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
int min=Integer.MAX_VALUE;


public MinStack() {
this.stack=new Stack<>();
this.minStack=new Stack<>();
minStack.push(min);

}

public void push(int val) {
min=Math.min(min,val);
stack.push(val);
minStack.push(min);

}

public void pop() {
stack.pop();
minStack.pop();
min=minStack.peek();

}

public int top() {
return stack.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();
*/