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


//Description:
// - Uses a 2D array as a hash table with two-level hashing.
// - The first hash selects a primary bucket, and the second hash selects an index within that bucket.
// - Buckets are created only when needed, allowing direct access to values in constant time.

class MyHashMap {
private static final int PRIMARY_BUCKET_SIZE = 1000;
private static final int SECONDARY_BUCKET_SIZE = 1000;

private final int[][] primaryBuckets;

public MyHashMap() {
this.primaryBuckets = new int[PRIMARY_BUCKET_SIZE][];
}

public void put(int key, int value) {
int primaryHash = getPrimaryHash(key);
if(this.primaryBuckets[primaryHash] == null) {
int secondaryBucketSize = SECONDARY_BUCKET_SIZE;
if(primaryHash == 0) {
secondaryBucketSize += 1;
}
this.primaryBuckets[primaryHash] = new int[secondaryBucketSize];
Arrays.fill(this.primaryBuckets[primaryHash], -1);
}
int secondaryHash = getSecondaryHash(key);
this.primaryBuckets[primaryHash][secondaryHash] = value;
}

public int get(int key) {
int primaryHash = getPrimaryHash(key);
if(this.primaryBuckets[primaryHash] == null) {
return -1;
}
int secondaryHash = getSecondaryHash(key);

return this.primaryBuckets[primaryHash][secondaryHash];
}

public void remove(int key) {
int primaryHash = getPrimaryHash(key);
if(this.primaryBuckets[primaryHash] == null) {
return;
}

int secondaryHash = getSecondaryHash(key);

this.primaryBuckets[primaryHash][secondaryHash] = -1;
}

private int getPrimaryHash(int key) {
return key % PRIMARY_BUCKET_SIZE;
}

private int getSecondaryHash(int key) {
return key / SECONDARY_BUCKET_SIZE;
}
}
45 changes: 45 additions & 0 deletions MyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Time Complexity : push: O(1), pop: O(n) in worst case, O(1) in amortized, peek: O(n), O(1) in amortized
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


//Description:
// - Uses two stacks:
// - stack1 stores newly added elements
// - stack2 serves elements in FIFO order
// - Elements are transferred from stack1 to stack2 only when stack2 is empty, which makes pop and peek amortized O(1).

class MyQueue {
private final Stack<Integer> stack1;
private final Stack<Integer> stack2;

public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}

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

public int pop() {
peek();

return stack2.pop();
}

public int peek() {
if(stack2.isEmpty()) {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}

return stack2.peek();
}

public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}