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
85 changes: 85 additions & 0 deletions HashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//Approach
//TC: O(1)
//SC: O(n)


class MyHashMap {
Node[] storage;
int buckets;

class Node {
int key;
int value;
Node next;

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

public MyHashMap() {
this.buckets = 1000;
this.storage = new Node[buckets];
}

public int getHash(int key) {
return key % buckets;
}

public 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 primary = getHash(key);
if (storage[primary] == null) {
storage[primary] = new Node(-1, -1);
storage[primary].next = new Node(key, value);
return;
}
Node prev = getPrev(storage[primary], key);
if (prev.next == null) {
prev.next = new Node(key, value);
} else {
prev.next.value = value;
}
}

public int get(int key) {
int hash = getHash(key);
if (storage[hash] == null)
return -1;
Node prev = getPrev(storage[hash], key);
if (prev.next == null) {
return -1;
}
return prev.next.value;
}

public void remove(int key) {
int hash = getHash(key);
if (storage[hash] == null) {
return;
}
Node prev = getPrev(storage[hash], key);
if (prev.next == null) {
return;
}
prev.next = prev.next.next;
}
}

/**
* 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);
*/
50 changes: 50 additions & 0 deletions QueueImpleWithStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.*;

// Approach:
// TC:O(1), worstcase - O(n)
// SC:O(n)
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 (outSt.isEmpty()) {
while (!inSt.isEmpty()) {
outSt.push(inSt.pop());
}
}
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();
}
}

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