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 232. Implement Queue using Stacks/MyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.*;

class MyQueue {

Stack<Integer> inStack;
Stack<Integer> outStack;

public MyQueue() {

this.inStack = new Stack<>();
this.outStack = new Stack<>();

}

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

public int pop() {

if (outStack.isEmpty() && inStack.isEmpty()) {
return -1;
}

peek();
return outStack.pop();

}

public int peek() {

if (outStack.isEmpty()) {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
return outStack.peek();
}

public boolean empty() {

return inStack.isEmpty() && outStack.isEmpty();

}
}

// TC = O(n)
// SC = O(n)

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
78 changes: 78 additions & 0 deletions 706. Design HashMap/MyHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.util.*;

class MyHashMap {

int primaryBucket;
int secondaryBucket;
int[][] storage;

public MyHashMap() {

this.primaryBucket = 1000;
this.secondaryBucket = 1000;
this.storage = new int[primaryBucket +1][];

}

public int getPrimaryHash(int key) {

return key / primaryBucket;

}

public int getSecondaryHash(int key) {

return key % secondaryBucket;

}

public void put(int key, int value) {

int primaryIndex = getPrimaryHash(key);

if (storage[primaryIndex] == null) {
if (primaryIndex == 0) {
storage[primaryIndex] = new int[secondaryBucket + 1];
} else {
storage[primaryIndex] = new int[secondaryBucket];
}
Arrays.fill(storage[primaryIndex], -1);
}

int secondaryIndex = getSecondaryHash(key);
storage[primaryIndex][secondaryIndex] = value;

}

public int get(int key) {

int primaryIndex = getPrimaryHash(key);
if (storage[primaryIndex] == null)
return -1;

int secondaryIndex = getSecondaryHash(key);
return storage[primaryIndex][secondaryIndex];

}

public void remove(int key) {

int primaryIndex = getPrimaryHash(key);
if (storage[primaryIndex] == null)
return;

int secondaryIndex = getSecondaryHash(key);
storage[primaryIndex][secondaryIndex] = -1;
}
}

// TC - O(1)
// SC - O(10^6) //As it is demmand on allocation so actual is O(1)

/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.put(key,value);
* int param_2 = obj.get(key);
* obj.remove(key);
*/