From cde0db9bfc714f6b874c4fb5abf72ad1366f1083 Mon Sep 17 00:00:00 2001 From: sainathek Date: Mon, 12 Jan 2026 01:38:47 -0500 Subject: [PATCH 1/2] Done Design-1 --- solution1.java | 0 solution2.java | 106 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 solution1.java create mode 100644 solution2.java diff --git a/solution1.java b/solution1.java new file mode 100644 index 00000000..e69de29b diff --git a/solution2.java b/solution2.java new file mode 100644 index 00000000..66898e54 --- /dev/null +++ b/solution2.java @@ -0,0 +1,106 @@ +// Time Complexity : +// put(key, value) -> Average: O(1), Worst: O(n) (if many keys fall in same bucket) +// get(key) -> Average: O(1), Worst: O(n) +// remove(key) -> Average: O(1), Worst: O(n) +// (n = number of nodes in that bucket) +// +// Space Complexity : +// O(N + B) where N = total pairs stored, B = number of buckets +// +// Did this code successfully run on Leetcode : +// Yes +// +// Any problem you faced while coding this : +// No major issues. Just need to handle collisions using a LinkedList, +// and update value if key already exists. + + +// Your code here along with comments explaining your approach + +import java.util.LinkedList; + +class MyHashMap { + + // total buckets size + int parentbuckets; + + // each index has a linkedlist of nodes (to handle collisions) + LinkedList[] storage; + + // simple node class to store key-value + static class Node { + int key; + int value; + Node(int key, int value) { + this.key = key; + this.value = value; + } + } + + public MyHashMap() { + this.parentbuckets = 1000; + storage = new LinkedList[parentbuckets]; + } + + // hash function to find bucket index + private int getPrimaryHash(int key) { + return Math.floorMod(key, parentbuckets); + } + + public void put(int key, int value) { + int index = getPrimaryHash(key); + + // if bucket not created, create it + if (storage[index] == null) { + storage[index] = new LinkedList<>(); + } + + // check if key already exists -> update + for (Node n : storage[index]) { + if (n.key == key) { + n.value = value; + return; + } + } + + // if key not found, add new node + storage[index].add(new Node(key, value)); + } + + public int get(int key) { + int index = getPrimaryHash(key); + + // if bucket empty, key not present + if (storage[index] == null) return -1; + + // search in that bucket + for (Node n : storage[index]) { + if (n.key == key) return n.value; + } + + return -1; + } + + public void remove(int key) { + int index = getPrimaryHash(key); + + // if bucket doesn't exist, nothing to remove + if (storage[index] == null) return; + + // find and remove the node + for (Node n : storage[index]) { + if (n.key == key) { + storage[index].remove(n); + break; + } + } + } +} + +/** + * 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); + */ From b08412af057e4e3e618e185911faf56e00644dfc Mon Sep 17 00:00:00 2001 From: sainathek Date: Mon, 12 Jan 2026 01:46:10 -0500 Subject: [PATCH 2/2] Done Design-2 --- solution1.java | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/solution1.java b/solution1.java index e69de29b..9e056a89 100644 --- a/solution1.java +++ b/solution1.java @@ -0,0 +1,75 @@ +// Time Complexity : +// push(x) -> O(1) +// pop() -> Amortized O(1) +// peek() -> Amortized O(1) +// empty() -> O(1) +// +// Space Complexity : +// O(N) where N is number of elements stored in the queue +// +// Did this code successfully run on Leetcode : +// Yes +// +// Any problem you faced while coding this : +// No major issues. The only thing to be careful about is moving elements +// from one stack to another only when needed, otherwise operations become slow. + + +// Your code here along with comments explaining your approach + +import java.util.Stack; + +class MyQueue { + + // This stack is used for push operation + private Stack inst; + + // This stack is used for pop and peek operations + private Stack outst; + + public MyQueue() { + this.inst = new Stack<>(); + this.outst = new Stack<>(); + } + + public void push(int x) { + // Always push element into input stack + inst.push(x); + } + + public int pop() { + // If output stack is empty, move all elements from input stack to output stack + if (outst.isEmpty()) { + while (!inst.isEmpty()) { + outst.push(inst.pop()); + } + } + // Now top of outst is the front of the queue + return outst.pop(); + } + + public int peek() { + // If output stack is empty, move elements first + if (outst.isEmpty()) { + while (!inst.isEmpty()) { + outst.push(inst.pop()); + } + } + // Return the front element + return outst.peek(); + } + + public boolean empty() { + // Queue is empty only if both stacks are empty + return inst.isEmpty() && outst.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(); + */