From 331f2dadbfda76f6d9f0579962850cae475f0aff Mon Sep 17 00:00:00 2001 From: Aiswaryaa Venugopalan Date: Tue, 22 Oct 2024 23:59:52 -0700 Subject: [PATCH 1/2] Done design 2 --- Sample.java | 7 ------- min_stack_155.py | 0 2 files changed, 7 deletions(-) delete mode 100644 Sample.java create mode 100644 min_stack_155.py diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cb..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach diff --git a/min_stack_155.py b/min_stack_155.py new file mode 100644 index 00000000..e69de29b From 29734fa04b23e2a984208056ad8b6203e7a4ca6e Mon Sep 17 00:00:00 2001 From: Aiswaryaa Venugopalan Date: Wed, 23 Oct 2024 23:09:09 -0700 Subject: [PATCH 2/2] Done problem 4, 5 --- design_hashmap_706.py | 52 ++++++++++++++++++++++++++++++++++++++++ min_stack_155.py | 20 ++++++++++++++++ queue_using_stack_232.py | 26 ++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 design_hashmap_706.py create mode 100644 queue_using_stack_232.py diff --git a/design_hashmap_706.py b/design_hashmap_706.py new file mode 100644 index 00000000..39ae68e5 --- /dev/null +++ b/design_hashmap_706.py @@ -0,0 +1,52 @@ +class ListNode: + def __init__(self, key, value): + self.key = key + self.value = value + self.next = None + +class MyHashMap: + + def __init__(self): + self.size = 1000 + self.table = [None] * self.size + + def _index(self, key: int) -> int: + return key % self.size + + def put(self, key: int, value: int) -> None: + idx = self._index(key) + if not self.table[idx]: + self.table[idx] = ListNode(key, value) + return + current = self.table[idx] + while current: + if current.key == key: + current.value = value + return + if not current.next: + current.next = ListNode(key, value) + return + current = current.next + + def get(self, key: int) -> int: + idx = self._index(key) + current = self.table[idx] + while current: + if current.key == key: + return current.value + current = current.next + return -1 + + def remove(self, key: int) -> None: + idx = self._index(key) + current = self.table[idx] + if not current: + return + if current.key == key: + self.table[idx] = current.next + return + while current.next: + if current.next.key == key: + current.next = current.next.next + return + current = current.next diff --git a/min_stack_155.py b/min_stack_155.py index e69de29b..a09cd69d 100644 --- a/min_stack_155.py +++ b/min_stack_155.py @@ -0,0 +1,20 @@ +class MinStack: + + def __init__(self): + self.s = [] + self.minstack = [] + + def push(self, val: int) -> None: + self.s.append(val) + min_val = min(val, self.minstack[-1] if self.minstack else val) + self.minstack.append(min_val) + + def pop(self) -> None: + self.s.pop() + self.minstack.pop() + + def top(self) -> int: + return self.s[-1] + + def getMin(self) -> int: + return self.minstack[-1] \ No newline at end of file diff --git a/queue_using_stack_232.py b/queue_using_stack_232.py new file mode 100644 index 00000000..8d62322e --- /dev/null +++ b/queue_using_stack_232.py @@ -0,0 +1,26 @@ +class MyQueue: + + def __init__(self): + self.stack1 = [] + self.stack2 = [] + + def push(self, x: int) -> None: + self.stack1.append(x) + while self.stack1: + self.stack2.append(self.stack1.pop()) + + def pop(self) -> int: + return self.stack2.pop(0) + + def peek(self) -> int: + return self.stack2[0] + + def empty(self) -> bool: + return not self.stack2 + +# 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