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
89 changes: 89 additions & 0 deletions DesignHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// O(1) time, O(n) space

// Linked List class
class Node {
int key;
int value;
Node next;

public Node(int key, int value) {
this.key = key;
this.value = value;
this.next = null;
}
}

class DesignHashMap {

int primaryBuckets = 10000;
Node[] table;

public DesignHashMap() {
table = new Node[primaryBuckets];
}

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

private Node getPrev(Node head, int key) {
Node prev = null;
Node curr = head;

while (curr != null && curr.key != key) {
prev = curr;
curr = curr.next;
}

return prev;
}

public void put(int key, int value) {
int index = getHash(key);

if (table[index] == null) {
table[index] = new Node(-1, -1); // dummy node
table[index].next = new Node(key, value); // assign dummy.next to incoming node
return;
}

Node prev = getPrev(table[index], key);

if (prev.next == null) {
prev.next = new Node(key, value);
}
else {
prev.next.value = value; // if key exists, update value
}

}

public int get(int key) {
int index = getHash(key);

if (table[index] == null) {
return -1;
}

Node prev = getPrev(table[index], key);
if (prev.next == null) {
return -1;
}
return prev.next.value;
}

public void remove(int key) {
int index = getHash(key);

if (table[index] == null) {
return;
}

Node prev = getPrev(table[index], key);
if (prev.next == null) {
return;
}
prev.next = prev.next.next; // update pointer

}
}
39 changes: 39 additions & 0 deletions QueueUsingStacks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.Stack;

// O(1) time, O(n) space
public class QueueUsingStacks {
Stack<Integer> inStack;
Stack<Integer> outStack;

public QueueUsingStacks() {
inStack = new Stack<>();
outStack = new Stack<>();
}

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

public int pop() {
if (outStack.isEmpty()) {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
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();
}
}