From ae0024a29b82b71443ce477826e68854d44a62cf Mon Sep 17 00:00:00 2001 From: Rajj Date: Wed, 23 Oct 2024 16:51:26 -0500 Subject: [PATCH] Completed Design-2 --- MyHashMap.java | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ MyQueue.java | 63 +++++++++++++++++++++++++++++ 2 files changed, 170 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..a79bb3b5 --- /dev/null +++ b/MyHashMap.java @@ -0,0 +1,107 @@ +// Time Complexity :put: O(1) +// get: O(1) +// remove: O(1) +// Space Complexity :put: O(1) +// get: O(1) +// remove: O(1) +// constructor: O(n) (because we are creating storage array) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : nothing + + + +class MyHashMap { + + class Node { + int key; + int value; + Node next; + + Node(int key, int value){ + this.key = key; + this.value = value; + } + + } + + Node[] storage; + int buckets; + + public MyHashMap() { + this.buckets=10000; + this.storage = new Node[buckets]; + } + + private int getPrimaryHash(int key){ + return key % buckets; + } + + private Node getPrevNode(Node head, int key){ + Node prev = null; + Node curr = head; + + while(curr != null && curr.key != key){ + prev = curr; + curr = prev.next; + } + return prev; + } + + public void put(int key, int value) { + int pIndex = getPrimaryHash(key); + if(storage[pIndex] == null){ + storage[pIndex] = new Node(-1,-1); + } + Node prev= getPrevNode(storage[pIndex], key); + if(prev.next == null){ + prev.next = new Node(key , value); + } else { + prev.next.value = value; + } + + } + + public int get(int key) { + int pIndex = getPrimaryHash(key); + if(storage[pIndex] == null){ + return -1; + } + Node prev = getPrevNode(storage[pIndex], key); + if(prev.next == null){ + return -1; + } else { + return prev.next.value; + } + } + + public void remove(int key) { + int pIndex = getPrimaryHash(key); + if(storage[pIndex]== null){ + return; + } + Node prev = getPrevNode(storage[pIndex], key); + if(prev.next == null){ + return; + } else { + Node temp = prev.next; + prev.next = temp.next; + temp = null; + } + } + + + public static void main(String args[]) + { + MyHashMap obj = new MyHashMap(); + obj.put(2,10); + obj.put(4,10); + obj.put(2,12); + obj.put(5,30); + obj.put(6,3000); + obj.put(7,4000); + obj.remove(4); + System.out.println("Value corresponding to key 6 is "+obj.get(6)); + + } +} + diff --git a/MyQueue.java b/MyQueue.java new file mode 100644 index 00000000..8f672ba3 --- /dev/null +++ b/MyQueue.java @@ -0,0 +1,63 @@ +// Time Complexity : empty: O(1) +// push: O(1) +// pop: O(1) +// peek: O(1) +// Space Complexity : empty: O(1) +// push: O(1) +// pop: O(1) +// peek: O(1) +// constructor: O(2n) because we're creating stacks +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : nothing + +import java.util.Stack; + +class MyQueue { + Stack inStk; + Stack outStk; + public MyQueue() { + this.inStk= new Stack<>(); + this.outStk= new Stack<>(); + } + + public void push(int x) { + inStk.push(x); + } + + public int pop() { + if(empty()) return -1; + if(outStk.isEmpty()){ + while(!inStk.isEmpty()){ + outStk.push(inStk.pop()); + } + } + return outStk.pop(); + } + + public int peek() { + if(outStk.isEmpty()){ + while(!inStk.isEmpty()){ + outStk.push(inStk.pop()); + } + } + return outStk.peek(); + } + + public boolean empty() { + return inStk.isEmpty() && outStk.isEmpty(); + } + + + public static void main(String args[]) + { + MyQueue queue = new MyQueue(); + queue.push(10); + queue.push(20); + queue.push(30); + queue.push(9); + queue.push(7); + queue.push(6); + System.out.println(queue.pop() + " Popped from stack"); + System.out.println(queue.peek() + " is top element of the stack"); + } +}