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
94 changes: 94 additions & 0 deletions MyHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
class MyHashMap {

// Time Complexity :
// search = O(1)
// get = O(1)
// put = O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

/*
Using Node array as the data structure to store key value pair and next Node pointer.
To search a node exists, we travel upto the node which matches value and keep track of the prev node
and return the prev node. The reason to return prev node is to make sure that when
deleting a node we have access to the prev node as we cannot back track. We call search to
get the previous of the node we want to add, delete, or get. For delete, we just need to set prev
element to point to next of the next element (which we want to delete). To add or update, we just
add new node to next of the prev node if null or update value of the next node.
*/

class Node {
int key, val;
Node next;

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

private Node[] storage;

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

private int hash(int key) {
return key % 10000;
}

private Node search(Node head, int key) {
Node prev = null;
Node cur = head;
while (cur != null && cur.key != key) {
prev = cur;
cur = cur.next;
}
return prev;
}

public void put(int key, int value) {
int hashKey = hash(key);
if (storage[hashKey] == null) {
storage[hashKey] = new Node(-1, -1);
}
Node prev = search(storage[hashKey], key);
if (prev.next != null) {
prev.next.val = value;
} else {
prev.next = new Node(key, value);
}
}

public int get(int key) {
int hashKey = hash(key);
if (storage[hashKey] == null) {
return -1;
}
Node prev = search(storage[hashKey], key);
if (prev.next != null) {
return prev.next.val;
}
return -1;
}

public void remove(int key) {
int hashKey = hash(key);
if (storage[hashKey] == null) {
return;
}
Node prev = search(storage[hashKey], key);
if (prev.next != null) {
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);
*/
60 changes: 60 additions & 0 deletions MyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.*;

class MyQueue {

// Time Complexity :
// peek = avg O(1), worst O(n)
// pop = avg O(1), worst O(n)
// push = O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

/*
to implement queue using two stack array we have in stack where we push the value
and out stack to pop the value from. Since queue is a FIFO data structure,
to pop the first element we added, we need to move all the elements from in stck to out stack to pop them.
We handle this under peek function, which checks if out stack is empty then push elements
from in to out stack and then returns the top most element. pop function calls peek func to populate the
out stack and then pops the topmost element. For push function, we can directly push to the in stack since the in stack already has the order in which we insert the elements
*/

private Stack<Integer> in;
private Stack<Integer> out;

public MyQueue() {
this.in = new Stack<>();
this.out = new Stack<>();
}

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

public int pop() {
peek();
return out.pop();
}

public int peek() {
if (out.empty()) {
while (!in.empty()) {
out.push(in.pop());
}
}
return out.peek();
}

public boolean empty() {
return out.empty() && in.empty();
}
}

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