diff --git a/p1 designhashset.py b/p1 designhashset.py new file mode 100644 index 00000000..9d3a5753 --- /dev/null +++ b/p1 designhashset.py @@ -0,0 +1,55 @@ +class MyHashSet: + + def __init__(self): + self.primaryBuckets = 1000 + self.secondaryBuckets = 1000 + # Initialize storage with None to save memory (Lazy Loading) + self.storage = [None] * self.primaryBuckets + + def primaryHash(self, key): + return key % self.primaryBuckets + + def secondaryHash(self, key): + return key // self.secondaryBuckets + + def add(self, key: int) -> None: + pIdx = self.primaryHash(key) + sIdx = self.secondaryHash(key) + + if self.storage[pIdx] is None: + # For the 0th index, we need 1001 to accommodate key 1,000,000 + if pIdx == 0: + self.storage[pIdx] = [False] * (self.secondaryBuckets + 1) + else: + self.storage[pIdx] = [False] * self.secondaryBuckets + + self.storage[pIdx][sIdx] = True + + def remove(self, key: int) -> None: + pIdx = self.primaryHash(key) + sIdx = self.secondaryHash(key) + + if self.storage[pIdx] is not None: + self.storage[pIdx][sIdx] = False + + def contains(self, key: int) -> bool: + pIdx = self.primaryHash(key) + sIdx = self.secondaryHash(key) + + if self.storage[pIdx] is None: + return False + return self.storage[pIdx][sIdx] + +# --- Example Execution --- +if __name__ == "__main__": + hashSet = MyHashSet() + print("Action: add(1), add(2)") + hashSet.add(1) + hashSet.add(2) + + print(f"contains(1): {hashSet.contains(1)}") # True + print(f"contains(3): {hashSet.contains(3)}") # False + + print("Action: remove(2)") + hashSet.remove(2) + print(f"contains(2): {hashSet.contains(2)}") # False \ No newline at end of file diff --git a/p2 designminstack.py b/p2 designminstack.py new file mode 100644 index 00000000..becd17d2 --- /dev/null +++ b/p2 designminstack.py @@ -0,0 +1,42 @@ +class MinStack: + def __init__(self): + self.st = [] + self.minst = [] + self.min = float('inf') + self.minst.append(self.min) + + def push(self, val: int) -> None: + self.min = min(val, self.min) + self.st.append(val) + self.minst.append(self.min) + + def pop(self) -> None: + if self.st: + self.st.pop() + self.minst.pop() + self.min = self.minst[-1] + + def top(self) -> int: + return self.st[-1] if self.st else None + + def getMin(self) -> int: + # In your image, this was partially cut off. + # It returns the current minimum from the auxiliary stack. + return self.minst[-1] if len(self.minst) > 1 else None + +# --- Example Execution --- +if __name__ == "__main__": + minStack = MinStack() + + print("Action: push(-2), push(0), push(-3)") + minStack.push(-2) + minStack.push(0) + minStack.push(-3) + + print(f"getMin(): {minStack.getMin()}") # Returns -3 + + print("Action: pop()") + minStack.pop() + + print(f"top(): {minStack.top()}") # Returns 0 + print(f"getMin(): {minStack.getMin()}") # Returns -2 \ No newline at end of file