From f8e13588988be0d1c08eed3edeaa642e78db1329 Mon Sep 17 00:00:00 2001 From: Manasi Burange Date: Tue, 22 Oct 2024 22:32:54 -0700 Subject: [PATCH 1/2] Completed Design-2 Problems --- 2024_Problem4_Impl_Queue_using_Stack.java | 51 +++++++++++++++++++++++ 2024_Problem5_Design_HashMap.java | 0 2 files changed, 51 insertions(+) create mode 100644 2024_Problem4_Impl_Queue_using_Stack.java create mode 100644 2024_Problem5_Design_HashMap.java diff --git a/2024_Problem4_Impl_Queue_using_Stack.java b/2024_Problem4_Impl_Queue_using_Stack.java new file mode 100644 index 00000000..4084139c --- /dev/null +++ b/2024_Problem4_Impl_Queue_using_Stack.java @@ -0,0 +1,51 @@ +//232. Implement Queue using Stacks - https://leetcode.com/problems/implement-queue-using-stacks/description/ +//Time Complexity: All operation takes constant time O(1) +//Space Complexity: O(n) + +class MyQueue { + + Stack stack; + Stack reverseStack; + + public MyQueue() { + this.stack = new Stack<>(); + this.reverseStack = new Stack<>(); + } + + public void push(int x) { + stack.push(x); // Push element x to the end + } + + private void reverseStack(){ + while(!stack.empty()){ + reverseStack.push(stack.pop()); //pop elements from main stack and push in reverseStack in reverse order + } + } + + /** Removes the element from in front of queue and returns that element. */ + public int pop() { + int popElement = -1; //default value of stack if empty + + if(reverseStack.empty()) + reverseStack(); //populate the reverse stack + + popElement = reverseStack.pop(); //poping the first element in queue + return popElement; + } + + /** Get the front element. */ + public int peek() { + int peekElement = -1; //default value if stack is empty + + if(reverseStack.empty()) + reverseStack(); //populate the reverse stack + + peekElement = reverseStack.peek(); //peeking the first element in queue w/o poping + return peekElement; + } + + /** Returns whether the queue is empty. */ + public boolean empty() { + return (stack.empty() && reverseStack.empty()); + } +} \ No newline at end of file diff --git a/2024_Problem5_Design_HashMap.java b/2024_Problem5_Design_HashMap.java new file mode 100644 index 00000000..e69de29b From baf3cad3f348cf6e3adcaf9b5f12f2bf05135413 Mon Sep 17 00:00:00 2001 From: Manasi Burange Date: Thu, 24 Oct 2024 05:12:16 -0700 Subject: [PATCH 2/2] Completed Design-2 Problem5 --- 2024_Problem5_Design_HashMap.java | 76 +++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/2024_Problem5_Design_HashMap.java b/2024_Problem5_Design_HashMap.java index e69de29b..d7355340 100644 --- a/2024_Problem5_Design_HashMap.java +++ b/2024_Problem5_Design_HashMap.java @@ -0,0 +1,76 @@ +class MyHashMap { + + //Here primary DS is array so we need to store the Node to get the secondary DS + class Node{ + int key; + int value; + Node next; + + public Node(int key, int value){ + this.key = key; + this.value = value; + } + } + + Node[] storage; + int buckets; + + public MyHashMap() { + //increased the primary data structure size to reduce the secondary DS + //this way way we are reducing no. of collisions + this.buckets = 10000; + this.storage = new Node[buckets]; + } + private int getPrimaryHash(int key){ + return key % buckets; + } + + private Node getPrev(Node head, int key){ + Node prev = null; + Node curr = head; + while(curr != null && curr.key != key){ + prev = curr; + curr = curr.next; + } + return prev; + } + + public void put(int key, int value) { + int primaryIndex = getPrimaryHash(key); + if(storage[primaryIndex] == null){ + storage[primaryIndex] = new Node(-1,-1); //creating dummy node to get prev node for the 1st Node + } + Node prev = getPrev(storage[primaryIndex],key); + if(prev.next == null){ //last node and couldn't find the node + prev.next = new Node(key,value); + } else { + prev.next.value = value; + } + } + + public int get(int key) { + int primaryIndex = getPrimaryHash(key); + if(storage[primaryIndex] == null){ + return -1; + } + Node prev = getPrev(storage[primaryIndex],key); + if(prev.next == null){ + return -1; + } + return prev.next.value; + } + + public void remove(int key) { + int primaryIndex = getPrimaryHash(key); + if(storage[primaryIndex] == null){ + return; + } + Node prev = getPrev(storage[primaryIndex],key); + if(prev.next == null){ + return; + } + Node curr = prev.next; + prev.next=curr.next; + curr.next = null; + } +} \ No newline at end of file