-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomPickwithBlacklist.cpp
More file actions
38 lines (35 loc) · 915 Bytes
/
randomPickwithBlacklist.cpp
File metadata and controls
38 lines (35 loc) · 915 Bytes
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
// Source: https://leetcode.com/problems/random-pick-with-blacklist/
// Author: Miao Zhang
// Date: 2021-03-03
class Solution {
public:
Solution(int N, vector<int>& blacklist) {
unordered_set<int> st;
int blen = 0;
if (!blacklist.empty()) {
blen = blacklist.size();
}
wlen = N - blen;
cout << wlen << endl;
for (int i = wlen; i < N; i++) {
st.insert(i);
}
for (int n: blacklist) st.erase(n);
auto it = st.begin();
for (int n: blacklist) {
if (n < wlen) m[n] = *it++;
}
}
int pick() {
int k = rand() % wlen;
return m.count(k) ? m[k] : k;
}
private:
unordered_map<int, int> m;
int wlen;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(N, blacklist);
* int param_1 = obj->pick();
*/