From dd38bba9d3c5716364bf8752bbf2658dbf90d05a Mon Sep 17 00:00:00 2001 From: CHINMAY-PRAJAPATI Date: Sun, 19 Apr 2026 16:54:39 -0400 Subject: [PATCH] Desig-2 Completed --- hashmap.py | 56 ++++++++++++++++++++++++++++++++++ implement_queue_using_stack.py | 38 +++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 hashmap.py create mode 100644 implement_queue_using_stack.py diff --git a/hashmap.py b/hashmap.py new file mode 100644 index 00000000..316d744c --- /dev/null +++ b/hashmap.py @@ -0,0 +1,56 @@ + +""" +@author: chinmayprajapati + +Time: O(1) +Space: O(n + k) + +""" + +class ListNode: + def __init__(self, key=-1, val=-1, next=None): + self.key = key + self.val = val + self.next = next + +class MyHashMap: + + def __init__(self): + self.map = [ListNode() for i in range(1000)] + + def hash(self, key): + return key % len(self.map) + + def put(self, key: int, value: int) -> None: + current = self.map[self.hash(key)] + while current.next: + if current.next.key == key: + current.next.val = value + return + current = current.next + current.next = ListNode(key, value) + + def get(self, key: int) -> int: + current = self.map[self.hash(key)].next + while current: + if current.key == key: + return current.val + current = current.next + return -1 + + def remove(self, key: int) -> None: + current = self.map[self.hash(key)] + while current and current.next: + if current.next.key == key: + current.next = current.next.next + return + current = current.next + + + + +# Your MyHashMap object will be instantiated and called as such: +# obj = MyHashMap() +# obj.put(key,value) +# param_2 = obj.get(key) +# obj.remove(key) \ No newline at end of file diff --git a/implement_queue_using_stack.py b/implement_queue_using_stack.py new file mode 100644 index 00000000..0c8a77dd --- /dev/null +++ b/implement_queue_using_stack.py @@ -0,0 +1,38 @@ + +""" +@author: chinmayprajapati + +Time: O(1) amortized +Space: O(n) +""" + +class MyQueue: + + def __init__(self): + self.instack = list() + self.outstack = list() + + def push(self, x: int) -> None: + self.instack.append(x) + + def pop(self) -> int: + self.peek() + return self.outstack.pop() + + def peek(self) -> int: + if len(self.outstack) == 0: + while len(self.instack) != 0: + self.outstack.append(self.instack.pop()) + return self.outstack[-1] + + def empty(self) -> bool: + return len(self.instack) == 0 and len(self.outstack) == 0 + + + +# Your MyQueue object will be instantiated and called as such: +# obj = MyQueue() +# obj.push(x) +# param_2 = obj.pop() +# param_3 = obj.peek() +# param_4 = obj.empty() \ No newline at end of file