forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random-pick-with-blacklist.cpp
69 lines (61 loc) · 1.56 KB
/
random-pick-with-blacklist.cpp
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: ctor: O(b)
// pick: O(1)
// Space: O(b)
class Solution {
public:
Solution(int N, vector<int> blacklist) :
n_(N - blacklist.size()) {
unordered_set<int> whitelist;
for (int i = n_; i < N; ++i) {
whitelist.emplace(i);
}
for (const auto& black : blacklist) {
whitelist.erase(black);
}
auto white = whitelist.cbegin();
for (const auto& black : blacklist) {
if (black < n_) {
lookup_[black] = *(white++);
}
}
}
int pick() {
int index = rand() % n_;
return lookup_.count(index) ? lookup_[index] : index;
}
private:
int n_;
unordered_map<int, int> lookup_;
};
// Time: ctor: O(blogb)
// pick: O(logb)
// Space: O(b)
class Solution2 {
public:
Solution(int N, vector<int> blacklist) :
n_(N - blacklist.size()),
blacklist_(blacklist) {
sort(blacklist_.begin(), blacklist_.end());
}
int pick() {
int index = rand() % n_;
int left = 0, right = blacklist_.size() - 1;
while (left <= right) {
auto mid = left + (right - left) / 2;
if (index + mid < blacklist_[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return index + left;
}
private:
int n_;
vector<int> blacklist_;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(N, blacklist);
* int param_1 = obj.pick();
*/