From f66a05049963d0e4c61a84a4b57ed2c9dfa75acd Mon Sep 17 00:00:00 2001 From: Vaishnavi Gawale Date: Sat, 27 Dec 2025 23:24:05 -0500 Subject: [PATCH] Done Design1 --- MinStack.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ MyHashSet.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 MinStack.py create mode 100644 MyHashSet.py diff --git a/MinStack.py b/MinStack.py new file mode 100644 index 00000000..4e953104 --- /dev/null +++ b/MinStack.py @@ -0,0 +1,47 @@ +''' Time Complexity : O(1) for all the operations + Space Complexity : O(n) ; where n is no of elements and we are maintaining the stack + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No + + Your code here along with comments explaining your approach + + Approach : Maintaining the one to one mapping between stack and min_stack, + to keep the track of previous minimums. +''' + + +class MinStack: + + def __init__(self): + self.stack = [] + self.min_stack = [] + self.min=float("infinity") + + def push(self, val: int) -> None: + self.stack.append(val) + if val < self.min: + self.min=val + self.min_stack.append(self.min) + + def pop(self) -> None: + self.stack.pop() + self.min_stack.pop() + if self.min_stack: + self.min = self.min_stack[-1] + else: + self.min = float("infinity") + + + def top(self) -> int: + return self.stack[-1] + + def getMin(self) -> int: + return self.min_stack[-1] + + +# Your MinStack object will be instantiated and called as such: +# obj = MinStack() +# obj.push(val) +# obj.pop() +# param_3 = obj.top() +# param_4 = obj.getMin() \ No newline at end of file diff --git a/MyHashSet.py b/MyHashSet.py new file mode 100644 index 00000000..76309976 --- /dev/null +++ b/MyHashSet.py @@ -0,0 +1,51 @@ +''' Time Complexity : O(1) for all the operations + Space Complexity : O(n) ; where n is no of elements will be added to the hashset + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No + + + Your code here along with comments explaining your approach + + Approach : First, initialized the storage with primary array and boolean array, then + implemented the first and second hash function. +''' + +class MyHashSet: + + def __init__(self): + self.bucket = 1000 + self.bucketItem = 1001 + self.storage = [[] for i in range(self.bucket)] + + def add(self, key: int) -> None: + + index = key%self.bucket + if self.storage[index] == []: + self.storage[index] = [False for i in range(self.bucketItem)] + + index2 = key//self.bucketItem + self.storage[index][index2] = True + + def remove(self, key: int) -> None: + index = key%self.bucket + if self.storage[index]: + index2 = key//self.bucketItem + self.storage[index][index2] = False + + def contains(self, key: int) -> bool: + index = key%self.bucket + index2 = key//self.bucketItem + if self.storage[index] != []: + return self.storage[index][index2] + else: + return False + +obj = MyHashSet() +print(obj.add(1)) +print(obj.add(2)) +print(obj.contains(1)) +print(obj.contains(3)) +print(obj.add(2)) +print(obj.contains(2)) +print(obj.remove(2)) +print(obj.contains(2))