Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design1 solved #2177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
71 changes: 71 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.util.Stack;

/*
Problem 2:
Design MinStack (https://leetcode.com/problems/min-stack/)
*/
public class MinStack {
Stack<Integer> st;
Stack<Integer> minSt;
int min;
public MinStack() {
this.st = new Stack<>();
this.minSt = new Stack<>();
this.min = Integer.MAX_VALUE;
minSt.push(min);
}

public void push(int val) {
min = Math.min(val,min);
st.push(val);
minSt.push(min);
}

public void pop() {
st.pop();
minSt.pop();
min = minSt.peek();
}

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

public int getMin() {
return minSt.peek();
}
}

class MinStack2 {
Stack<Integer> st;
int min;
public MinStack2() {
this.st = new Stack<>();

this.min = Integer.MAX_VALUE;

}

public void push(int val) {
if(min>=val){
st.push(min);
}
min = Math.min(val,min);
st.push(val);
}

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

}

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

public int getMin() {
return min;
}
}
55 changes: 55 additions & 0 deletions MyHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//https://leetcode.com/problems/design-hashset/
public class MyHashSet {
int primarySize ;
int secondarySize;
boolean[][] data;

public MyHashSet() {
this.primarySize = 1001;
this.secondarySize = 1000;
this.data = new boolean[primarySize][];
}

public void add(int key) {
int primaryHash = getPrimaryHash(key);
if(this.data[primaryHash]==null){
this.data[primaryHash] = new boolean [secondarySize];
}
int secondaryHash = getSecondaryHash(key);
this.data[primaryHash][secondaryHash] = true;
}

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

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

}

public int getPrimaryHash(int key){
return key/this.primarySize;
}
public int getSecondaryHash(int key){
return key%this.secondarySize;
}
}

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