From 6d97e60a02432a0290f4c6b839e0af47251748bb Mon Sep 17 00:00:00 2001 From: LeelaTotapally Date: Wed, 8 Apr 2026 18:27:57 -0400 Subject: [PATCH 1/2] Implemented Queue using Stack --- QueueUsingStack.java | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 QueueUsingStack.java diff --git a/QueueUsingStack.java b/QueueUsingStack.java new file mode 100644 index 00000000..1a6154fb --- /dev/null +++ b/QueueUsingStack.java @@ -0,0 +1,66 @@ +// Time Complexity : O(1) for push, O(n) for pop and peek in worst case when outStack is empty, O(1) for pop and peek when outStack is not empty. +// 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 +/** + * We can use two stacks to implement a queue. + * We will push elements to stack, and when we need to pop or peek, we will check if the outStack is empty. If it is empty, we will pop all elements from inStack and push them to outStack. + * This way, the order of elements will be reversed and we can pop or peek from outStack to get the front element of the queue. + * To check empty, we will check if both stacks are empty. + */ + + +class MyQueue { + Stack inStack = new Stack<>(); + Stack outStack = new Stack<>(); + + public MyQueue() { + + } + + public void push(int x) { + inStack.push(x); + + } + + public void reverse(){ + if((outStack.isEmpty())){ + while(!(inStack.isEmpty())){ + outStack.push(inStack.pop()); + + } + } + + } + + public int pop() { + reverse(); + return outStack.pop(); + + + } + + public int peek() { + reverse(); + return outStack.peek(); + + + } + + public boolean empty() { + return outStack.isEmpty() && inStack.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(); + */ From 71b3929eaf47538537b7772ec88e8d944c3444ec Mon Sep 17 00:00:00 2001 From: LeelaTotapally Date: Thu, 9 Apr 2026 18:50:55 -0400 Subject: [PATCH 2/2] HashMap using Double hashing --- Hashmap.java | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Hashmap.java diff --git a/Hashmap.java b/Hashmap.java new file mode 100644 index 00000000..b12684e7 --- /dev/null +++ b/Hashmap.java @@ -0,0 +1,73 @@ +// Time Complexity : O(1) for put, get and remove operations +// Space Complexity : O(n) where n is the number of unique keys +// 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 +/** + * The row index will be calculated using the modulus operator and the column index will be calculated using the division operator. + * This way, we can store up to 1000000 unique keys in the hash map. + * To handle collisions, we will initialize the second dimension of the array with a size of 1001, so that we can store values for keys that have the same row index. + * Since the value 0 is used to indicate that a key is not present in the hash map, we will store the value as value+1 to differentiate between a key that is not present and a key that has a value of 0. + */ +import java.util.*; +class MyHashMap { + int array[][] = new int[1000][]; + + public MyHashMap() { + + + } + public int row(int key){ + return key%1000; + + } + + public int column(int key){ + return key/1000; + + } + + public void put(int key, int value) { + int row = row(key); + int column = column(key); + if(array[row]==null){ + array[row] = new int[1001]; + } + array[row][column] = value+1; + + + + + } + + public int get(int key) { + int row = row(key); + int column = column(key); + int value = -1; + if (array[row] !=null){ + return array[row][column]-1; + } + return -1; + + } + + public void remove(int key) { + int row = row(key); + int column = column(key); + if( array[row] !=null){ + array[row][column] =0; + } + + + } +} + +/** + * 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); + */ \ No newline at end of file