From 8a5b305f797d8176919851c43b067c1d9beea8b6 Mon Sep 17 00:00:00 2001 From: agk-s30 Date: Thu, 30 Jul 2026 01:21:48 -0700 Subject: [PATCH 1/2] Add MinStack class implementation Implement MinStack class with push, pop, top, and getMin methods. --- Exercise_2.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Exercise_2.py diff --git a/Exercise_2.py b/Exercise_2.py new file mode 100644 index 00000000..d4fa3d55 --- /dev/null +++ b/Exercise_2.py @@ -0,0 +1,29 @@ +# Min Stack +# https://leetcode.com/problems/min-stack/description/ + +# Time complexity: O(1) +# Space complexity: O(n) + +# Uses an additional stack to maintain the current minimum value. Push to min_stack when change in min value or equal. Similarly, pop when min value is popped. + +class MinStack: + + def __init__(self): + self.stack = [] + self.min_stack = [] + + def push(self, value: int) -> None: + self.stack.append(value) + if not self.min_stack or value <= self.min_stack[-1]: + self.min_stack.append(value) + + def pop(self) -> None: + if self.min_stack[-1] == self.stack[-1]: + self.min_stack.pop() + self.stack.pop() + + def top(self) -> int: + return self.stack[-1] + + def getMin(self) -> int: + return self.min_stack[-1] From 6152b4123cace935890c665dbbd9b36ed8206dd0 Mon Sep 17 00:00:00 2001 From: agk-s30 Date: Thu, 30 Jul 2026 01:41:48 -0700 Subject: [PATCH 2/2] Add MyHashSet implementation with basic operations Implement a MyHashSet class with add, remove, and contains methods using double hashing. --- Exercise_1.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Exercise_1.py diff --git a/Exercise_1.py b/Exercise_1.py new file mode 100644 index 00000000..31313eea --- /dev/null +++ b/Exercise_1.py @@ -0,0 +1,57 @@ +# Design HashSet +# https://leetcode.com/problems/design-hashset/description/ + +# Time complexity: O(1) +# Space complexity: O(n) + +# Uses double hashing - primary hash to find which bucket, secondary hash to find position in bucket; +# also uses boolean values instead of int to save space - works well here because this is a set and the operations only require "check if exisits" operations + +class MyHashSet: + + def __init__(self): + self.primary_buckets = 1000 + self.secondary_buckets = 1001 + + # Each primary bucket is initialized only when needed. + self.storage = [None] * self.primary_buckets + + def _primary_hash(self, key: int) -> int: + return key % self.primary_buckets + + def _secondary_hash(self, key: int) -> int: + return key // self.primary_buckets + + def add(self, key: int) -> None: + primary_index = self._primary_hash(key) + secondary_index = self._secondary_hash(key) + + if self.storage[primary_index] is None: + self.storage[primary_index] = [False] * self.secondary_buckets + + self.storage[primary_index][secondary_index] = True + + def remove(self, key: int) -> None: + primary_index = self._primary_hash(key) + + if self.storage[primary_index] is None: + return + + secondary_index = self._secondary_hash(key) + self.storage[primary_index][secondary_index] = False + + def contains(self, key: int) -> bool: + primary_index = self._primary_hash(key) + + if self.storage[primary_index] is None: + return False + + secondary_index = self._secondary_hash(key) + return self.storage[primary_index][secondary_index] + + +# Your MyHashSet object will be instantiated and called as such: +# obj = MyHashSet() +# obj.add(key) +# obj.remove(key) +# param_3 = obj.contains(key)