From 6d76489c20f458bc46dbbb23a8ad5dff8018ed14 Mon Sep 17 00:00:00 2001 From: Shinjanee Gupta Date: Wed, 4 Feb 2026 13:11:24 -0800 Subject: [PATCH 1/2] Design1 --- HashSet.py | 39 +++++++++++++++++++++++++++++++++++++++ MinStack.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 HashSet.py create mode 100644 MinStack.py diff --git a/HashSet.py b/HashSet.py new file mode 100644 index 00000000..3bf79754 --- /dev/null +++ b/HashSet.py @@ -0,0 +1,39 @@ +# Time Complexity : O(1) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach : two level bucket structure (primary -> secondary) with boolean arrays for constant time access. + +class MyHashSet: + def __init__(self): + self.primaryBucketSize = 1000 + self.secondaryBucketSize = 1001 # for division edge case + self.storage = [[] for _ in range(self.primaryBucketSize)] + + def getPrimaryHash(self, key: int) -> int: + return key % self.primaryBucketSize + + def getSecondaryHash(self, key: int) -> int: + return key // self.secondaryBucketSize + + def add(self, key: int) -> None: + primaryHash = self.getPrimaryHash(key) + if self.storage[primaryHash] == []: + self.storage[primaryHash] = [False] * (self.secondaryBucketSize) + + secondaryHash = self.getSecondaryHash(key) + self.storage[primaryHash][secondaryHash] = True + + def remove(self, key: int) -> None: + primaryHash = self.getPrimaryHash(key) + if not self.storage[primaryHash]: + return + secondaryHash = self.getSecondaryHash(key) + self.storage[primaryHash][secondaryHash] = False + + def contains(self, key: int) -> bool: + primaryHash = self.getPrimaryHash(key) + if not self.storage[primaryHash]: + return False + secondaryHash = self.getSecondaryHash(key) + return self.storage[primaryHash][secondaryHash] \ No newline at end of file diff --git a/MinStack.py b/MinStack.py new file mode 100644 index 00000000..34920cc0 --- /dev/null +++ b/MinStack.py @@ -0,0 +1,28 @@ +# Time Complexity : O(1) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach : Only when a new min appears, we store the previous min in the stack, +# so that when the current min is removed, we can restore the old one in constant time. + +class MinStack: + + def __init__(self): + self.stack = [] + self.min = float('inf') + + def push(self, val: int) -> None: + if val <= self.min: + self.stack.append(self.min) + self.min = val + self.stack.append(val) + + def pop(self) -> None: + if self.min == self.stack.pop(): + self.min = self.stack.pop() + + def top(self) -> int: + return self.stack[-1] + + def getMin(self) -> int: + return self.min \ No newline at end of file From d26fbecbceb3c208f19a0eaf039a31e6b738abcd Mon Sep 17 00:00:00 2001 From: Shinjanee Gupta Date: Wed, 4 Feb 2026 19:49:08 -0800 Subject: [PATCH 2/2] updated --- HashSet.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/HashSet.py b/HashSet.py index 3bf79754..72ddd856 100644 --- a/HashSet.py +++ b/HashSet.py @@ -7,7 +7,7 @@ class MyHashSet: def __init__(self): self.primaryBucketSize = 1000 - self.secondaryBucketSize = 1001 # for division edge case + self.secondaryBucketSize = 1000 self.storage = [[] for _ in range(self.primaryBucketSize)] def getPrimaryHash(self, key: int) -> int: @@ -19,14 +19,17 @@ def getSecondaryHash(self, key: int) -> int: def add(self, key: int) -> None: primaryHash = self.getPrimaryHash(key) if self.storage[primaryHash] == []: - self.storage[primaryHash] = [False] * (self.secondaryBucketSize) + if primaryHash == 0: + self.storage[primaryHash] = [False] * (self.secondaryBucketSize + 1) # handle division edge case for 10^6 + else: + self.storage[primaryHash] = [False] * (self.secondaryBucketSize) secondaryHash = self.getSecondaryHash(key) self.storage[primaryHash][secondaryHash] = True def remove(self, key: int) -> None: primaryHash = self.getPrimaryHash(key) - if not self.storage[primaryHash]: + if self.storage[primaryHash] == []: return secondaryHash = self.getSecondaryHash(key) self.storage[primaryHash][secondaryHash] = False