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

//Hashmap implementation with array of type Node (linked list)



// Time Complexity : time complexity of O(1) , it doesn't traverse more than 100 items per operation
// Space Complexity : allocating O(n) node spaces
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : wiritng getPrev and handling null pointer exception is something took time for me.

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;
this.next = null;
}
}

public MyHashMap() {

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


}

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

}

public void remove(int key) {
int index = getHash(key);
if(storage[index] == null ){
return;
}
if( getPrev(storage[index], key).next == null ){
return ;
} else {
getPrev(storage[index], key).next = getPrev(storage[index], key).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);
*/
74 changes: 74 additions & 0 deletions Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//Problem 1: (https://leetcode.com/problems/implement-queue-using-stacks/)

// Implement Queue using 2 Stacks
//

// Time Complexity : Amortized O(1) time complexity for pop and peek
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : checking if outStack and is empty and instack is not empty condition took some time for me
// followed the same approach as in class

class MyQueue {
Stack<Integer> inStack ;
Stack<Integer> outStack;


public MyQueue() {

inStack = new Stack<Integer>();
outStack = new Stack<Integer>();

}

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

public int pop() {
if (outStack.isEmpty()) {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}

if (outStack.isEmpty()) {
return -1; // queue is empty
}

return outStack.pop();

}

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

if (outStack.isEmpty()) {
return -1; // queue is empty
}

return outStack.peek();

}

public boolean empty() {
if(inStack.isEmpty() && outStack.isEmpty()){
return true;
}
return false;

}
}

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