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
69 changes: 69 additions & 0 deletions Design HashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :


// Your code here along with comments explaining your approach



//Leetcode 706. Design HashMap

class MyHashSet {
int primaryb;
int secondaryb;
boolean storage[][];
public MyHashSet() {
this.primaryb = 1000;
this.secondaryb = 1000;
this.storage = new boolean[primaryb][];
}
private int getprimaryhash(int key){
return key%primaryb;
}
private int getsecondaryhash(int key){
return key/secondaryb;
}

public void add(int key) {
int primaryIndex = getprimaryhash(key);

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

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

public void remove(int key) {
int primaryIndex = getprimaryhash(key);

if(storage[primaryIndex] == null){
return;
}

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

public boolean contains(int key) {
int primaryIndex= getprimaryhash(key);
if(storage[primaryIndex]==null){return false;}
int secondaryIndex = getsecondaryhash(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);
*/
41 changes: 41 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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;
}

public void push(int val) {
min = Math.min(min,val);
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 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();
*/
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.