forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign-hashset.java
More file actions
69 lines (54 loc) · 2.4 KB
/
design-hashset.java
File metadata and controls
69 lines (54 loc) · 2.4 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
68
69
// Time Complexity : O(1) for add, remove, and contains (average case)
// Space Complexity : O(1) effectively (fixed bucket size, not dependent on number of elements inserted)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Handling edge case for key = 10^6 required allocating extra space for primaryIndex = 0 bucket.
// Approach:
// Use a 2D boolean array to simulate a hash set with double hashing.
// First hash (mod) decides the bucket, second hash (division) decides the position inside the bucket.
// Lazy initialization is used to save space, and a special case is handled for index 0 to accommodate max key value.
class MyHashSet {
int primaryBuckets;
int secondaryBuckets;
boolean[][] storage;
public MyHashSet() {
this.primaryBuckets = 1000;
this.secondaryBuckets = 1000;
this.storage = new boolean[primaryBuckets][];
}
// Primary hash: distributes keys across 1000 buckets
private int getPrimaryHashKey(int key) {
return key % primaryBuckets;
}
// Secondary hash: position inside each bucket
private int getSecondaryHashKey(int key) {
return key / secondaryBuckets;
}
public void add(int key) {
int primaryIndex = getPrimaryHashKey(key);
// Lazy initialization of bucket to save space
if (storage[primaryIndex] == null) {
// Special case for key = 10^6 (falls into bucket 0)
if (primaryIndex == 0) {
storage[primaryIndex] = new boolean[secondaryBuckets + 1];
} else {
storage[primaryIndex] = new boolean[secondaryBuckets];
}
}
int secondaryIndex = getSecondaryHashKey(key);
storage[primaryIndex][secondaryIndex] = true;
}
public void remove(int key) {
int primaryIndex = getPrimaryHashKey(key);
// If bucket not initialized, key doesn't exist
if (storage[primaryIndex] == null) return;
int secondaryIndex = getSecondaryHashKey(key);
storage[primaryIndex][secondaryIndex] = false;
}
public boolean contains(int key) {
int primaryIndex = getPrimaryHashKey(key);
// If bucket not initialized, key doesn't exist
if (storage[primaryIndex] == null) return false;
int secondaryIndex = getSecondaryHashKey(key);
return storage[primaryIndex][secondaryIndex];
}
}