From 5a1341560b544c0c6fcbbb1271548c380e47f01c Mon Sep 17 00:00:00 2001 From: Aditya Bhuran Date: Wed, 3 Jun 2026 20:36:16 -0400 Subject: [PATCH 1/2] Add HashSet and MinStack solutions --- design-1.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 design-1.py diff --git a/design-1.py b/design-1.py new file mode 100644 index 000000000..1c7f73fb5 --- /dev/null +++ b/design-1.py @@ -0,0 +1,70 @@ +# Problem 1: Design HashSet +# Time Complexity: O(1) for all operations - add, remove, contains +# Space Complexity: O(n) n is number of unique keys +# Did this code successfully run on Leetcode: YES +# Any problem you faced while coding this: Faced error in some syntax + +class MyHashSet(object): + def __init__(self): + self.initial_bucket = 1000 + self.secondary_bucket = 1000 + self.storage = [None] * self.initial_bucket + + def hash_func1(self, key): + return key % self.initial_bucket + + def hash_func2(self, key): + return key // self.secondary_bucket + + def add(self, key): + bucket = self.hash_func1(key) + internal_bucket = self.hash_func2(key) + if self.storage[bucket] is None: + if bucket == 0: + self.storage[bucket] = [False] * (self.secondary_bucket + 1) + else: + self.storage[bucket] = [False] * self.secondary_bucket + self.storage[bucket][internal_bucket] = True + + def remove(self, key): + bucket = self.hash_func1(key) + internal_bucket = self.hash_func2(key) + if self.storage[bucket] is None: + return + self.storage[bucket][internal_bucket] = False + + def contains(self, key): + bucket = self.hash_func1(key) + internal_bucket = self.hash_func2(key) + if self.storage[bucket] is None: + return False + return self.storage[bucket][internal_bucket] + + +# Problem 2: Min Stack +# Time Complexity: O(1) for all operations - push, pop, top, getMin +# Space Complexity: O(n) +# Did this code successfully run on Leetcode: YES, but needed to take reference from solution to understand the approach +# Any problem you faced while coding this: Understanding the two stack approach + +class MinStack: + def __init__(self): + self.main_stack = [] + self.min_stack = [] + + def push(self, val: int) -> None: + self.main_stack.append(val) + if not self.min_stack or val <= self.min_stack[-1]: + self.min_stack.append(val) + else: + self.min_stack.append(self.min_stack[-1]) + + def pop(self) -> None: + self.main_stack.pop() + self.min_stack.pop() + + def top(self) -> int: + return self.main_stack[-1] + + def getMin(self) -> int: + return self.min_stack[-1] \ No newline at end of file From edcbd4e47b3b855260d6e4a6cb9004b91b5f7273 Mon Sep 17 00:00:00 2001 From: Aditya Bhuran Date: Wed, 3 Jun 2026 22:27:28 -0400 Subject: [PATCH 2/2] min-stack seperate file --- min-stack.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 min-stack.py diff --git a/min-stack.py b/min-stack.py new file mode 100644 index 000000000..5784efb4e --- /dev/null +++ b/min-stack.py @@ -0,0 +1,27 @@ +# Problem 2: Min Stack +# Time Complexity: O(1) for all operations - push, pop, top, getMin +# Space Complexity: O(n) +# Did this code successfully run on Leetcode: YES, but needed to take reference from solution to understand the approach +# Any problem you faced while coding this: Understanding the two stack approach + +class MinStack: + def __init__(self): + self.main_stack = [] + self.min_stack = [] + + def push(self, val: int) -> None: + self.main_stack.append(val) + if not self.min_stack or val <= self.min_stack[-1]: + self.min_stack.append(val) + else: + self.min_stack.append(self.min_stack[-1]) + + def pop(self) -> None: + self.main_stack.pop() + self.min_stack.pop() + + def top(self) -> int: + return self.main_stack[-1] + + def getMin(self) -> int: + return self.min_stack[-1] \ No newline at end of file