forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
design_hashset.py
52 lines (41 loc) · 1.91 KB
/
design_hashset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class MyHashSet(object):
def __init__(self):
self.primary_buckets = 1000
self.secondary_buckets = 1000
# creates a 2D array of size 1000
self.storage = [[False] for _ in range(self.primary_buckets)]
def get_primary_hash(self, key):
return key % self.primary_buckets
def get_secondary_hash(self, key):
return key // self.secondary_buckets
def add(self, key):
# O(1) is the best time complexity and O(n) is the worst time complexity
primary_index = self.get_primary_hash(key)
if len(self.storage[primary_index]) == 1:
if primary_index == 0:
self.storage[primary_index] = [False] * (self.secondary_buckets + 1)
else:
self.storage[primary_index] = [False] * self.secondary_buckets
secondary_index = self.get_secondary_hash(key)
self.storage[primary_index][secondary_index] = True
def remove(self, key):
# O(1) is the best time complexity and O(1) is the worst time complexity
primary_index = self.get_primary_hash(key)
# If value of primary bucket at the above primary_index is empty, return
if len(self.storage[primary_index]) == 1:
return
secondary_index = self.get_secondary_hash(key)
self.storage[primary_index][secondary_index] = False
def contains(self, key):
# O(1) is the best time complexity and O(1) is the worst time complexity
primary_index = self.get_primary_hash(key)
# If value of primary bucket at the above primary_index is empty, return False
if len(self.storage[primary_index]) == 1:
return False
secondary_index = self.get_secondary_hash(key)
return self.storage[primary_index][secondary_index]
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)