diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..763821d3 --- /dev/null +++ b/problem1.py @@ -0,0 +1,58 @@ +# Design-1 + +## Problem 1:(https://leetcode.com/problems/design-hashset/) + +# Time Complexity : O(1) +# Space Complexity : O(n) +# 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 + + +class MyHashSet: + # bruteforce have an array of 10**6 each item will be added to idx = func(item % 10 ** 6) + # q1 what ds and size-> explain concept of hashing -> linear probing, double hashing , chaining + # why double hashing is good and not linear probing othat meand if collision go store in nearst empty not # good chaining also doent mean const lookup , why double hashing and also why dividing both arrays in wual is best + # this is double hashing where first has gives bucket and second hash gives the loc of idx in that bucket not initializing all to 10^6 spaces 10^3 primary array and then each contain null only when value arrives expanf each bucket to point to 10^3 array + + def __init__(self): + self.size = 1000 + self.arr = [None for _ in range(self.size)] + + def getBucket(self,key: int) -> int: + return key % self.size + + def getSecondArrPos(self,key:int) -> int: + return key // self.size + + def add(self, key: int) -> None: + idx = self.getBucket(key) + arrPos = self.getSecondArrPos(key) + if self.arr[idx] is None: + self.arr[idx] = [False] * self.size if idx != 0 else [False] * (self.size+1) + self.arr[idx][arrPos] = True + return + + def remove(self, key: int) -> None: + idx = self.getBucket(key) + if self.arr[idx] is None: + return + arrPos = self.getSecondArrPos(key) + self.arr[idx][arrPos] = False + return + + def contains(self, key: int) -> bool: + idx = self.getBucket(key) + if self.arr[idx] is None: + return False + arrPos = self.getSecondArrPos(key) + return self.arr[idx][arrPos] == True + + +# 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..56dc6c52 --- /dev/null +++ b/problem2.py @@ -0,0 +1,47 @@ +# Problem 2: +# Design MinStack (https://leetcode.com/problems/min-stack/) + +# Time Complexity : O(1) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : yes +# Any problem you faced while coding this :no + +class MinStack: + # since O(1) all we can have 2 stacks where when we push we push value to 1 and curr_min to anothercurr_min will update if curr_val < curr_min and then pop from both stacks so we can access curr_min at any time looking at min stack peek + # approach2: use 1 stack where do we fail if we try simple approach of having a min variable and a single stack ? we fail when we pop out curr_min element then we did not know what was the prev min so can we do one thing whenever we have a new value <= curr_min we push the old min first and then on top of it push new value (think simple approach we would fail what was the prev min this adresses that) please note that this we should push old min even if new value is equal to the old min + #below in approach 2 + 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) + return + + def pop(self) -> None: + if not self.stack: + return + top_val = self.stack.pop() + if top_val == self.min: + self.min = self.stack.pop() + return + + + def top(self) -> int: + return self.stack[-1] + + def getMin(self) -> int: + return self.min + + + + +# 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