diff --git a/Design HashSet.py b/Design HashSet.py new file mode 100644 index 00000000..1d061220 --- /dev/null +++ b/Design HashSet.py @@ -0,0 +1,31 @@ +# Time Complexity : O(1) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : Still trying to figure out on how to improve the space complexity keeping the Time Complexity at O(1) + + +# Your code here along with comments explaining your approach + + +class MyHashSet: + + def __init__(self): + # Declaring an array with the size based on the max value given in the statement + self.size = 1000000 + self.array = [None for i in range(self.size)] + + def add(self, key: int) -> None: + # Create a Hash Function and use it as an index to add key + hashfunc = key % self.size + self.array[hashfunc] = key + + def remove(self, key: int) -> None: + # Create a Hash Function and use it as an index to remove key + hashfunc = key % self.size + self.array[hashfunc] = None + + def contains(self, key: int) -> bool: + # Create a Hash Function and use it as find the key + hashfunc = key % self.size + return self.array[hashfunc] == key + diff --git a/MinStack.py b/MinStack.py new file mode 100644 index 00000000..d334f9c6 --- /dev/null +++ b/MinStack.py @@ -0,0 +1,39 @@ +# Time Complexity : O(1) for all operations +# Space Complexity : O(n) for all operations +# 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 +## Using 2 lists with one storing the actual numbers and the 2nd storing the minimum value at every instance + + +class MinStack: + + def __init__(self): + self.Stack = [] + self.minStack = [] + self.min = float('inf') + self.minStack.append(self.min) + + def push(self, val: int) -> None: + self.min = min(val, self.min) + self.Stack.append(val) + self.minStack.append(self.min) + + def pop(self) -> None: + self.Stack.pop() + self.minStack.pop() + self.min = self.minStack[-1] + + def top(self) -> int: + return self.Stack[-1] + + def getMin(self) -> int: + return self.minStack[-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