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
88 changes: 88 additions & 0 deletions src/MyHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
I use an array of buckets where each bucket stores a linked list to handle hash collisions using separate chaining.
The key is mapped to a bucket index using a hash function (key % size), and all operations traverse only that bucket.
This gives average O(1) time for add, remove, and contains, with O(n) worst case if many keys collide.
Time Complexity (average):
add: O(1)
remove: O(1)
contains: O(1)
Worst case (many collisions in one bucket):
O(n)

Space Complexity:
O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
*/

class MyHashMap {
private class Node{
int key;
int value;
Node next;
public Node(int key, int value){
this.key =key;
this.value = value;
}
}
private static final int DEFAULT_CAPACITY = 1000;
private Node [] buckets;

public MyHashMap() {
buckets = new Node[DEFAULT_CAPACITY];
}
private int hash(int key){
return key % DEFAULT_CAPACITY;
}

public void put(int key, int value) {
int hash = hash(key);
Node cur = buckets[hash];
if(buckets[hash] == null){
buckets[hash] = new Node(key, value);
return;
}
while(true){
if(cur.key == key){
cur.value = value;
return;
}
if(cur.next == null) break;
cur = cur.next;
}
cur.next = new Node(key, value);
}

public int get(int key) {
int hash = hash(key);
Node cur = buckets[hash];
if(buckets[hash] == null){
return -1;
}
while(cur != null){
if(cur.key == key){
return cur.value;
}
cur = cur.next;
}
return -1;
}

public void remove(int key) {
int hash = hash(key);
Node cur = buckets[hash];
if (cur == null) return;
// remove head
if(cur.key == key){
buckets[hash] = cur.next;
return;
}
while(cur.next != null){
if(cur.next.key == key){
cur.next = cur.next.next;
return;
}
cur = cur.next;
}
}
}
40 changes: 40 additions & 0 deletions src/MyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.Stack;
/*
I use two stacks: an input stack for push operations and an output stack for pop/peek operations.
When output is empty, and we need to pop/peek, I move all elements from input to output which reverses the order and simulates FIFO.
Each element moves from input to output at most once, so operations are amortized O(1).
*/
class MyQueue {
private Stack<Integer> inStack;
private Stack<Integer> outStack;

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

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

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

}

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

public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
}
18 changes: 18 additions & 0 deletions src/TestHomework2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

void main() {
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1,1);
myHashMap.put(2,2);
System.out.println(myHashMap.get(1));
System.out.println(myHashMap.get(3));
myHashMap.put(2,1);
System.out.println(myHashMap.get(2));
myHashMap.remove(2);
System.out.println(myHashMap.get(2));
MyQueue myQueue = new MyQueue();
myQueue.push(1);
myQueue.push(2);
System.out.println(myQueue.peek());
System.out.println(myQueue.pop());
myQueue.empty();
}