Skip to content

Commit 8bd8a13

Browse files
committed
chore: Completed Design-1
1 parent 08beeac commit 8bd8a13

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

design_hashset.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Time Complexity : O(1)* - * could be O(n) if all n inserted values happen to fall in the same bucket, thus requiring us to scan through the entire array n-times
2+
# Space Complexity : O(n) - for n elements being added
3+
# Did this code successfully run on Leetcode : yes
4+
# Any problem you faced while coding this : no
5+
6+
7+
# Your code here along with comments explaining your approach
8+
# we initiated an array of empty arrays so we can add new values to them by using % bucket_size. in each bucket, we add/remove the same way we would a normal array.
9+
10+
11+
class MyHashSet:
12+
13+
# Time: O(1)
14+
# Space: O(1)
15+
def __init__(self):
16+
self.bucket_size = 1000001
17+
self.buckets = [[] for _ in range(self.bucket_size)]
18+
19+
# Time: O(1)*
20+
# Space: O(1)
21+
def add(self, key: int) -> None:
22+
if key not in self.buckets[key % self.bucket_size]:
23+
self.buckets[key % self.bucket_size].append(key)
24+
25+
# Time: O(1)*
26+
# Space: O(1)
27+
def remove(self, key: int) -> None:
28+
if key in self.buckets[key % self.bucket_size]:
29+
self.buckets[key % self.bucket_size].remove(key)
30+
31+
# Time: O(1)*
32+
# Space: O(1)
33+
def contains(self, key: int) -> bool:
34+
return key in self.buckets[key % self.bucket_size]
35+
36+
37+
# Your MyHashSet object will be instantiated and called as such:
38+
# obj = MyHashSet()
39+
# obj.add(key)
40+
# obj.remove(key)
41+
# param_3 = obj.contains(key)

design_minstack.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Time Complexity : O(1)
2+
# Space Complexity : O(n)
3+
# Did this code successfully run on Leetcode : yes
4+
# Any problem you faced while coding this : no, only understanding the concept but once i understood it, coding was straightforward
5+
6+
7+
# Your code here along with comments explaining your approach
8+
# It look me a little bit to understand minstack but I came to the conclusion that two lists are needed for it: one to keep track of a normal stack, and one to keep track of the minimum value for constant time retrieval. Once I understood that, the rest of the functions was pretty self explanatory and straighforward
9+
10+
11+
class MinStack:
12+
13+
def __init__(self):
14+
self.stack = []
15+
self.minStack = []
16+
17+
# Time: O(1)
18+
# Space: O(1)
19+
def push(self, val: int) -> None:
20+
self.stack.append(val)
21+
if len(self.minStack) == 0 or val <= self.minStack[-1]:
22+
self.minStack.append(val)
23+
24+
# Time: O(1)
25+
# Space: O(1)
26+
def pop(self) -> None:
27+
val = self.stack.pop()
28+
if val == self.minStack[-1]:
29+
self.minStack.pop()
30+
31+
# Time: O(1)
32+
# Space: O(1)
33+
def top(self) -> int:
34+
return self.stack[-1]
35+
36+
# Time: O(1)
37+
# Space: O(1)
38+
def getMin(self) -> int:
39+
return self.minStack[-1]
40+
41+
42+
# Your MinStack object will be instantiated and called as such:
43+
# obj = MinStack()
44+
# obj.push(val)
45+
# obj.pop()
46+
# param_3 = obj.top()
47+
# param_4 = obj.getMin()

0 commit comments

Comments
 (0)