forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode705.py
More file actions
67 lines (50 loc) · 1.84 KB
/
leetcode705.py
File metadata and controls
67 lines (50 loc) · 1.84 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 3 22:01:46 2026
@author: rishigoswamy
Approach:
----------
Since the key range is bounded (0 <= key <= 10^6), we can use a
two-level hashing.
The idea is to split the key into two parts:
- Primary index = key % 1000
- Secondary index = key // 1000
This maps each key uniquely to a position in a 2D boolean table.
The secondary array size is 1001 to handle the maximum key value
(1_000_000 // 1000 = 1000).
Because each (primary, secondary) pair uniquely represents a key,
there are no collisions.
Time Complexity:
----------------
add : O(1)
remove : O(1)
contains : O(1)
Space Complexity:
-----------------
O(n), due to the fixed-size 2D table used for direct addressing.
"""
class MyHashSet:
def __init__(self):
self.primaryArraySize = 1000
self.secondaryArraySize = 1001
self.table = [[False] * self.secondaryArraySize for _ in range(self.primaryArraySize)]
def hashKeys(self, key: int):
return [key % self.primaryArraySize, key // self.primaryArraySize ]
def add(self, key: int) -> None:
primaryKey = self.hashKeys(key)[0]
secondaryKey = self.hashKeys(key)[1]
self.table[primaryKey][secondaryKey] = True
def remove(self, key: int) -> None:
primaryKey = self.hashKeys(key)[0]
secondaryKey = self.hashKeys(key)[1]
self.table[primaryKey][secondaryKey] = False
def contains(self, key: int) -> bool:
primaryKey = self.hashKeys(key)[0]
secondaryKey = self.hashKeys(key)[1]
return self.table[primaryKey][secondaryKey]
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)