diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..aec59755 --- /dev/null +++ b/problem1.py @@ -0,0 +1,49 @@ +# Design-1 + +## Problem 1:(https://leetcode.com/problems/design-hashset/) + +class MyHashSet: + + def __init__(self): + self.primary_size = 1000 + self.secondary_size = 1000 + self.storage = [None]*1000 + + def getPrimaryHash(self,val): + return val%self.primary_size + + def getSecondaryHash(self,val): + return val // self.secondary_size + + def add(self, key: int) -> None: + # add only if it doesnt exist + primary_hash = self.getPrimaryHash(key) + secondary_hash = self.getSecondaryHash(key) + if self.storage[primary_hash] == None: + if primary_hash == 0: + self.storage[primary_hash] = [False]* (self.secondary_size+1) + else: + self.storage[primary_hash] = [False]*self.secondary_size + self.storage[primary_hash][secondary_hash] = True + + def remove(self, key: int) -> None: + primary_hash = self.getPrimaryHash(key) + secondary_hash = self.getSecondaryHash(key) + if self.storage[primary_hash] == None: + return None + else: + self.storage[primary_hash][secondary_hash] = False + + def contains(self, key: int) -> bool: + primary_hash = self.getPrimaryHash(key) + secondary_hash = self.getSecondaryHash(key) + if self.storage[primary_hash] == None: + return False + return self.storage[primary_hash][secondary_hash] + + +# Your MyHashSet object will be instantiated and called as such: +# obj = MyHashSet() +# obj.add(key) +# obj.remove(key) +# param_3 = obj.contains(key) \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..514b5a4a --- /dev/null +++ b/problem2.py @@ -0,0 +1,46 @@ +## Problem 2: +## Design MinStack (https://leetcode.com/problems/min-stack/) + + +class MinStack: + + def __init__(self): + self.main_stack = [] + self.min_stack = [] + self.min_val = float('inf') + self.min_stack.append(self.min_val) + + def push(self, val: int) -> None: + self.min_val = min(self.min_val,val) + self.main_stack.append(val) + self.min_stack.append(self.min_val) + + + def pop(self) -> None: + self.main_stack.pop() + self.min_stack.pop() + self.min_val = self.min_stack[-1] + + def top(self) -> int: + return self.main_stack[-1] + + def getMin(self) -> int: + min_val = self.min_stack[0] + for i in range(1,len(self.min_stack)): + if self.min_stack[i] < min_val: + min_val = self.min_stack[i] + return min_val + +# 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() + +# 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