-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.cpp
50 lines (48 loc) · 1.19 KB
/
hash.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
class Solution {
public:
map<int,int> res;
int len;
Solution(int n, vector<int>& blacklist) {
// set<int> st;
// for(int i = 0; i<n; i++){
// st.insert(i);
// }
// for(auto black:blacklist){
// st.erase(black);
// }
// int cot = 0;
// for(auto x:st){
// res.insert(pair<int,int>(cot++,x));
// }
set<int> st;
len = n-blacklist.size();
for(int i = len; i<n; i++){
st.insert(i);
}
for(auto black:blacklist){
st.erase(black);
}
// for(auto i:st){
// cout<<i<<' ';
// }
// cout<<endl;
auto j = st.begin();
for(auto black:blacklist ){
if(black<len){
// cout<<*j<<endl;
// cout<<' '<<black<<endl;
res[black] = *j++;
}
}
}
int pick() {
int mid = rand()%len;
// cout<<mid<<endl;
return res.count(mid)?res[mid]:mid;
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(n, blacklist);
* int param_1 = obj->pick();
*/