From 6fb4a244ac0a43fcf094952a3bcaa3f3cc30abb5 Mon Sep 17 00:00:00 2001 From: Vaishnavi Gawale Date: Sun, 28 Dec 2025 21:13:26 -0500 Subject: [PATCH] Done Design-2 --- DesignHashmap.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++ QueueUsingStack.py | 42 +++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 DesignHashmap.py create mode 100644 QueueUsingStack.py diff --git a/DesignHashmap.py b/DesignHashmap.py new file mode 100644 index 00000000..db500d5e --- /dev/null +++ b/DesignHashmap.py @@ -0,0 +1,61 @@ +''' Time Complexity : Worst case for collision : O(n) + 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 + + Approach : Primary staorage as Array of type Node and secondary storage as linkedlist +''' +class ListNode: + def __init__(self,key=None,value=None,next=None): + self.key=key + self.value=value + self.next=next + +class MyHashMap: + + def __init__(self): + self.bucket=1000 + self.storage = [ListNode() for _ in range(self.bucket)] + + def put(self, key: int, value: int) -> None: + print("put :",key,value) + index = key % self.bucket + if self.storage[index]: + curr = self.storage[index] + while curr.next: + if curr.next.key == key: + curr.next.value = value + return + curr = curr.next + curr.next = ListNode(key, value) + + def get(self, key: int) -> int: + print("get:",key) + index = key % self.bucket + if self.storage[index]: + curr = self.storage[index] + while curr.next: + if curr.next.key == key: + return curr.next.value + curr = curr.next + return -1 + + def remove(self, key: int) -> None: + print("remove :",key) + index = key % self.bucket + if self.storage[index]: + curr = self.storage[index] + while curr.next: + if curr.next.key == key: + curr.next = curr.next.next + return + curr = curr.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/QueueUsingStack.py b/QueueUsingStack.py new file mode 100644 index 00000000..104d3d3c --- /dev/null +++ b/QueueUsingStack.py @@ -0,0 +1,42 @@ +''' Time Complexity : + push operation - 0(1) + pop, peek : O(1) amortized + 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 + + Approach : 1. Maintain 2 stack, inStack and minStack, push always in inStack + 2. For pop and peek, pop elements from inStack to minStack +''' + +class MyQueue: + def __init__(self): + self.in_stack=[] + self.out_stack=[] + + def push(self, x: int) -> None: + self.in_stack.append(x) + + def pop(self) -> int: + if not self.out_stack: + while self.in_stack: + self.out_stack.append(self.in_stack.pop()) + return self.out_stack.pop() + + def peek(self) -> int: + if not self.out_stack: + while self.in_stack: + self.out_stack.append(self.in_stack.pop()) + return self.out_stack[-1] + + def empty(self) -> bool: + return not self.in_stack and not self.out_stack + +# 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