diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cb..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach diff --git a/design_hashset.py b/design_hashset.py new file mode 100644 index 00000000..e96cd134 --- /dev/null +++ b/design_hashset.py @@ -0,0 +1,44 @@ +# Implemented using double hashing +# TC -> O(1) +# SC -> O(10^6) or O(N) + +class MyHashSet: + def __init__(self): + self.num_buckets = 1000 + self.buckets = [None] * self.num_buckets + + def add(self, key: int) -> None: + first_hash = self.first_hash(key) + second_hash = self.second_hash(key) + if self.buckets[first_hash] == None: + if first_hash == 0: + self.buckets[first_hash] = [False] * (self.num_buckets + 1) + else: + self.buckets[first_hash] = [False] * self.num_buckets + self.buckets[first_hash][second_hash] = True + return + def remove(self, key: int) -> None: + first_hash = self.first_hash(key) + second_hash = self.second_hash(key) + if self.buckets[first_hash]: + self.buckets[first_hash][second_hash] = False + return + def contains(self, key: int) -> bool: + first_hash = self.first_hash(key) + second_hash = self.second_hash(key) + if self.buckets[first_hash]: + return self.buckets[first_hash][second_hash] + return False + def first_hash(self, key) -> int: + return key % self.num_buckets + def second_hash(self, key) -> int: + return key // self.num_buckets + + + + +# 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/design_min_stack.py b/design_min_stack.py new file mode 100644 index 00000000..4bca02bd --- /dev/null +++ b/design_min_stack.py @@ -0,0 +1,32 @@ +# Use 2 stacks, one for the actual elements and the other to keep track of current min in the stack +# TC -> O(1) +# SC -> O(n) where n are the number of elemets to store in the stack + +class MinStack: + def __init__(self): + self.stack = [] + self.min_stack = [float("inf")] + + def push(self, value: int) -> None: + self.stack.append(value) + current_min = self.min_stack[-1] + self.min_stack.append(min(current_min, value)) + + def pop(self) -> None: + self.stack.pop() + self.min_stack.pop() + + 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(value) +# obj.pop() +# param_3 = obj.top() +# param_4 = obj.getMin() \ No newline at end of file