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
51 changes: 51 additions & 0 deletions MyHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class MyHashMap {
int primaryBuckets;
int secondaryBuckets;
int[][] storage;

public MyHashMap() {
this.primaryBuckets = 1000;
this.secondaryBuckets = 1000;
this.storage = new int[primaryBuckets][];
}

private int getPrimaryHash(int key){
return key % primaryBuckets;
}

private int getSecondaryHash(int key){
return key / secondaryBuckets;
}

public void put(int key, int value) {
int primaryIndex = getPrimaryHash(key);
if(storage[primaryIndex] == null){
if(primaryIndex == 0){
storage[primaryIndex] = new int[secondaryBuckets+1];
}else{
storage[primaryIndex] = new int[secondaryBuckets];
}
Arrays.fill(storage[primaryIndex], -1);
}
int secondaryIndex = getSecondaryHash(key);
storage[primaryIndex][secondaryIndex] = value;
}

public void remove(int key) {
int primaryIndex = getPrimaryHash(key);
if(storage[primaryIndex] == null){
return;
}
int secondaryIndex = getSecondaryHash(key);
storage[primaryIndex][secondaryIndex] = -1;
}

public int get(int key) {
int primaryIndex = getPrimaryHash(key);
if(storage[primaryIndex] == null){
return -1;
}
int secondaryIndex = getSecondaryHash(key);
return storage[primaryIndex][secondaryIndex];
}
}
32 changes: 32 additions & 0 deletions MyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class MyQueue {
Stack<Integer> inSt;
Stack<Integer> outSt;

public MyQueue() {
this.inSt = new Stack<>();
this.outSt = new Stack<>();
}

public void push(int x) {
inSt.push(x);
}

public int pop() {
if(empty()) return -1;
peek();
return outSt.pop();
}

public int peek() {
if(outSt.isEmpty()){
while(!inSt.isEmpty()){
outSt.push(inSt.pop());
}
}
return outSt.peek();
}

public boolean empty() {
return inSt.isEmpty() && outSt.isEmpty();
}
}