Skip to content

Commit 60a36e3

Browse files
committed
chore: Completed Design-1
1 parent 08beeac commit 60a36e3

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

design_hashset.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
# Time: O(1)
13+
# Space: O(1)
14+
def __init__(self):
15+
self.buckets = [[] for _ in range(1000)]
16+
17+
# Time: O(1)*
18+
# Space: O(1)
19+
def add(self, key: int) -> None:
20+
if key not in self.buckets[key % 1000]:
21+
self.buckets[key % 1000].append(key)
22+
23+
# Time: O(1)*
24+
# Space: O(1)
25+
def remove(self, key: int) -> None:
26+
if key in self.buckets[key % 1000]:
27+
self.buckets[key % 1000].remove(key)
28+
29+
# Time: O(1)*
30+
# Space: O(1)
31+
def contains(self, key: int) -> bool:
32+
return key in self.buckets[key % 1000]
33+
34+
35+
# Your MyHashSet object will be instantiated and called as such:
36+
# obj = MyHashSet()
37+
# obj.add(key)
38+
# obj.remove(key)
39+
# 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)