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
73 changes: 73 additions & 0 deletions Hashmap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Time Complexity : O(1) for put, get and remove operations
// Space Complexity : O(n) where n is the number of unique keys
// 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
/**
* The row index will be calculated using the modulus operator and the column index will be calculated using the division operator.
* This way, we can store up to 1000000 unique keys in the hash map.
* To handle collisions, we will initialize the second dimension of the array with a size of 1001, so that we can store values for keys that have the same row index.
* Since the value 0 is used to indicate that a key is not present in the hash map, we will store the value as value+1 to differentiate between a key that is not present and a key that has a value of 0.
*/
import java.util.*;
class MyHashMap {
int array[][] = new int[1000][];

public MyHashMap() {


}
public int row(int key){
return key%1000;

}

public int column(int key){
return key/1000;

}

public void put(int key, int value) {
int row = row(key);
int column = column(key);
if(array[row]==null){
array[row] = new int[1001];
}
array[row][column] = value+1;




}

public int get(int key) {
int row = row(key);
int column = column(key);
int value = -1;
if (array[row] !=null){
return array[row][column]-1;
}
return -1;

}

public void remove(int key) {
int row = row(key);
int column = column(key);
if( array[row] !=null){
array[row][column] =0;
}


}
}

/**
* 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);
*/
66 changes: 66 additions & 0 deletions QueueUsingStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Time Complexity : O(1) for push, O(n) for pop and peek in worst case when outStack is empty, O(1) for pop and peek when outStack is not empty.
// Space Complexity : 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
/**
* We can use two stacks to implement a queue.
* We will push elements to stack, and when we need to pop or peek, we will check if the outStack is empty. If it is empty, we will pop all elements from inStack and push them to outStack.
* This way, the order of elements will be reversed and we can pop or peek from outStack to get the front element of the queue.
* To check empty, we will check if both stacks are empty.
*/


class MyQueue {
Stack<Integer> inStack = new Stack<>();
Stack<Integer> outStack = new Stack<>();

public MyQueue() {

}

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

}

public void reverse(){
if((outStack.isEmpty())){
while(!(inStack.isEmpty())){
outStack.push(inStack.pop());

}
}

}

public int pop() {
reverse();
return outStack.pop();


}

public int peek() {
reverse();
return outStack.peek();


}

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