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
81 changes: 81 additions & 0 deletions MyHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Time Complexity : Amortized time complexity for operations - put, get, remove is O(1).
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

class MyHashMap {
class Node{
int key;
int value;
Node next;
public Node(int key, int value){
this.key = key;
this.value = value;
}
}
private Node [] storage;
private int hash(int key){
return key % 10000;
}

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

private Node find(Node head, int key){
Node previous = head;
Node current = head.next;
while(current!=null && current.key != key){
previous = current;
current = current.next;
}
return previous;
}

public void put(int key, int value) {
int itemIndex = hash(key);
if(storage[itemIndex] == null){
storage[itemIndex] = new Node(-1,-1);
}
Node previous = find(storage[itemIndex], key);
if(previous.next == null){
previous.next = new Node(key, value);
} else{
previous.next.value = value;
}
}

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

public void remove(int key) {
int itemIndex = hash(key);
if(storage[itemIndex] == null){
return;
}
Node previous = find(storage[itemIndex], key);
if(previous.next == null){
return;
}
Node temp = previous.next;
previous.next = temp.next;
temp.next = null;
}
}

/**
* 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);
*/
57 changes: 53 additions & 4 deletions Sample.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
// Time Complexity : Amortized time complexity for operations - push, pop, peek , Empty is O(1).
// Time Complexity : Worst case time complexity for operations - pop and peek is O(n) when the outStack is Empty.
// Space Complexity : 2 * O(n) = O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
class MyQueue {
private Stack<Integer> inStack;
private Stack<Integer> outStack;

public MyQueue() {
this.inStack = new Stack<>();
this.outStack = new Stack<>();
}

//Time Complexity - O(1)
public void push(int x) {
inStack.push(x);
}

//Time Complexity - O(1)
public int pop() {
if(outStack.isEmpty()){
while(!inStack.isEmpty()){
outStack.push(inStack.pop());
}
}
return outStack.pop();
}

//Time Complexity - O(1)
public int peek() {
if(outStack.isEmpty()){
while(!inStack.isEmpty()){
outStack.push(inStack.pop());
}
}
return outStack.peek();
}

//Time Complexity - O(1)
public boolean empty() {
return inStack.isEmpty() && outStack.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();
*/