forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.java
More file actions
65 lines (57 loc) · 1.95 KB
/
Sample.java
File metadata and controls
65 lines (57 loc) · 1.95 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
// Time Complexity :O(1)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this : yes, received ArrayOutOfBoundException while executing add function.
// Your code here along with comments explaining your approach
class MyHashSet {
int primaryBucket;
int secondaryBucket;
boolean[][] storage;
public MyHashSet() {
this.primaryBucket = 1000;
this.secondaryBucket = 1000;
this.storage = new boolean[primaryBucket][]; //storage initalised only for primaryBucket and when required we create a nested bucket
}
private int primaryHash(int key) {
// define hash function for primary bucket
return key % primaryBucket;
}
private int secondaryHash(int key) {
// // define hash function for secondary bucket
return key / secondaryBucket;
}
public void add(int key) {
int primaryIndex = primaryHash(key);
if (storage[primaryIndex] == null) {
// create a nested bucket
if (primaryIndex == 0) {
storage[primaryIndex] = new boolean[secondaryBucket + 1]; //to suffice the array out of bound exception at 0th index
} else {
storage[primaryIndex] = new boolean[secondaryBucket];
}
}
// when nested bucket already exists
int secondaryIndex = secondaryHash(key);
storage[primaryIndex][secondaryIndex] = true;
}
public void remove(int key) {
int primaryIndex = primaryHash(key);
if (storage[primaryIndex] == null) {
// nothing to remove
return;
}
// nested bucket exists
int secondaryIndex = secondaryHash(key);
storage[primaryIndex][secondaryIndex] = false;
}
public boolean contains(int key) {
int primaryIndex = primaryHash(key);
int secondaryIndex = secondaryHash(key);
if (storage[primaryIndex] == null) {
// no key present
return false;
}
// return the boolean present at the nested bucket location
return storage[primaryIndex][secondaryIndex];
}
}