From a74fb44359e1a998ed4fa76801749bf65d8dcd37 Mon Sep 17 00:00:00 2001 From: PritPanchani Date: Wed, 4 Feb 2026 10:16:49 -0800 Subject: [PATCH] Added MyQueue and MyHashMap solutions --- MyHashMap.java | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ MyQueue.java | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 MyHashMap.java create mode 100644 MyQueue.java diff --git a/MyHashMap.java b/MyHashMap.java new file mode 100644 index 00000000..d2540cc2 --- /dev/null +++ b/MyHashMap.java @@ -0,0 +1,67 @@ +// Time Complexity : O(1) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this : No + +// Approach: +// Use two-level indexing with primary and secondary hashing. +// Store values in a 2D array and use -1 to represent missing keys. + +class MyHashMap { + int primaryBuckets; + int secondaryBuckets; + int[][] storage; + + public MyHashMap() { + this.primaryBuckets = 1000; + this.secondaryBuckets = 1000; + this.storage = new int[primaryBuckets][]; + } + + private int getPrimaryHash(int key) { + return key % primaryBuckets; + } + + private int getSecondaryHash(int key) { + return key / secondaryBuckets; + } + + public void put(int key, int value) { + int primaryIndex = getPrimaryHash(key); + + if (storage[primaryIndex] == null) { + if (primaryIndex == 0) { + storage[primaryIndex] = new int[secondaryBuckets + 1]; + } else { + storage[primaryIndex] = new int[secondaryBuckets]; + } + + for (int i = 0; i < storage[primaryIndex].length; i++) { + storage[primaryIndex][i] = -1; + } + } + + int secondaryIndex = getSecondaryHash(key); + storage[primaryIndex][secondaryIndex] = value; + } + + public int get(int key) { + int primaryIndex = getPrimaryHash(key); + if (storage[primaryIndex] == null) { + return -1; + } + + int secondaryIndex = getSecondaryHash(key); + return storage[primaryIndex][secondaryIndex]; + } + + public void remove(int key) { + int primaryIndex = getPrimaryHash(key); + if (storage[primaryIndex] == null) { + return; + } + + int secondaryIndex = getSecondaryHash(key); + storage[primaryIndex][secondaryIndex] = -1; + } +} diff --git a/MyQueue.java b/MyQueue.java new file mode 100644 index 00000000..36158281 --- /dev/null +++ b/MyQueue.java @@ -0,0 +1,48 @@ +// Time Complexity : O(1) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Approach: +// Use one stack for push operations and another for pop/peek. +// Transfer elements only when needed to maintain FIFO order. + + +import java.util.Stack; + +class MyQueue { + Stack inputStack; + Stack outputStack; + + public MyQueue() { + this.inputStack = new Stack<>(); + this.outputStack = new Stack<>(); + } + + public void push(int x) { + inputStack.push(x); + } + + public int pop() { + if (outputStack.isEmpty()) { + while (!inputStack.isEmpty()) { + outputStack.push(inputStack.pop()); + } + } + return outputStack.pop(); + } + + public int peek() { + if (outputStack.isEmpty()) { + while (!inputStack.isEmpty()) { + outputStack.push(inputStack.pop()); + } + } + return outputStack.peek(); + } + + public boolean empty() { + return inputStack.isEmpty() && outputStack.isEmpty(); + } +}