From a74d9a5f71ec8c4c17ee97db6be477301115db5e Mon Sep 17 00:00:00 2001 From: pavanbollam Date: Fri, 10 Apr 2026 01:15:29 +0530 Subject: [PATCH 1/2] Done Design-2 --- Sample.java | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/Sample.java b/Sample.java index 1739a9cb..d70b13e5 100644 --- a/Sample.java +++ b/Sample.java @@ -1,7 +1,49 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : +// Time Complexity : O(1) +// 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 +// I use two stacks: mainStack for inputs and outStack for outputs. New elements are always pushed onto mainStack. For pop or peek operations, I only shift elements from mainStack to outStack if outStack is currently empty. This reversal correctly orders the elements for FIFO (First-In-First-Out) access and ensures an amortized O(1) time complexity, as each element is moved between stacks only once. + +import java.util.Stack; + +class MyQueue { + + private Stack mainStack; + private Stack outStack; + + public MyQueue() { + mainStack = new Stack<>(); + outStack = new Stack<>(); + } + + public void push(int x) { + mainStack.push(x); + } + + public int pop() { + shiftStacks(); + if (outStack.isEmpty()) return -1; // Or throw error + return outStack.pop(); + } + + public int peek() { + shiftStacks(); + if (outStack.isEmpty()) return -1; + return outStack.peek(); + } + + public boolean empty() { + return mainStack.isEmpty() && outStack.isEmpty(); + } + + private void shiftStacks() { + if (outStack.isEmpty()) { + while (!mainStack.isEmpty()) { + outStack.push(mainStack.pop()); + } + } + } +} \ No newline at end of file From 901f323bb26bce47234b39ecd58d53b06dd68a5c Mon Sep 17 00:00:00 2001 From: pavanbollam Date: Fri, 10 Apr 2026 23:16:45 +0530 Subject: [PATCH 2/2] Done desing-2 both problems --- Sample.java => ImplementQueueUsingStacks.java | 4 +- MyHashMap.java | 91 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) rename Sample.java => ImplementQueueUsingStacks.java (95%) create mode 100644 MyHashMap.java diff --git a/Sample.java b/ImplementQueueUsingStacks.java similarity index 95% rename from Sample.java rename to ImplementQueueUsingStacks.java index d70b13e5..22927992 100644 --- a/Sample.java +++ b/ImplementQueueUsingStacks.java @@ -9,12 +9,12 @@ import java.util.Stack; -class MyQueue { +class ImplementQueueUsingStacks { private Stack mainStack; private Stack outStack; - public MyQueue() { + public ImplementQueueUsingStacks() { mainStack = new Stack<>(); outStack = new Stack<>(); } diff --git a/MyHashMap.java b/MyHashMap.java new file mode 100644 index 00000000..ba0183eb --- /dev/null +++ b/MyHashMap.java @@ -0,0 +1,91 @@ +// Time Complexity : Time complexity is O(1) on average and O(n) in worst case +// 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 +// This custom HashMap uses an array of 1000 buckets where each bucket is a linked list to store key-value pairs, and it uses a simple hash function (key % 1000) to decide which bucket to put data in. When you add, get, or remove items, it finds the right bucket and then searches through the linked list to handle the operations. +class MyHashMap { + + static class Node { + private int key; + private int value; + private Node next; + + Node(int key, int value) { + this.key = key; + this.value = value; + this.next = null; + } + } + + private static final int SIZE = 1000; + private Node[] buckets; + + public MyHashMap() { + buckets = new Node[SIZE]; + } + + private int getHash(int key) { + return key % SIZE; + } + + public void put(int key, int value) { + int index = getHash(key); + if (buckets[index] == null) { + buckets[index] = new Node(-1, -1); + } + + Node previousNode = buckets[index]; + Node currentNode = previousNode.next; + + while (currentNode != null) { + if (currentNode.key == key) { + currentNode.value = value; + return; + } + previousNode = currentNode; + currentNode = currentNode.next; + } + + previousNode.next = new Node(key, value); + } + + public int get(int key) { + int index = getHash(key); + + if (buckets[index] == null) { + return -1; + } + + Node currentNode = buckets[index].next; + while (currentNode != null ){ + if (currentNode.key == key) { + return currentNode.value; + } + currentNode = currentNode.next; + } + return -1; + } + + public void remove(int key) { + int index = getHash(key); + + if (buckets[index] == null) { + return; + } + + Node previousNode = buckets[index]; + Node currentNode = previousNode.next; + + while (currentNode != null) { + if (currentNode.key == key) { + previousNode.next = currentNode.next; + return; + } + previousNode = currentNode; + currentNode = currentNode.next; + } + } +} \ No newline at end of file