-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
random-pick-index.cpp
50 lines (43 loc) · 1.11 KB
/
random-pick-index.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
// Time: ctor: O(n)
// pick: O(1)
// Space: O(n)
class Solution {
public:
Solution(vector<int> nums) : gen_(random_device()()) {
for (int i = 0; i < size(nums); ++i) {
lookup_[nums[i]].emplace_back(i);
}
}
int pick(int target) {
uniform_int_distribution<int> dist(0, size(lookup_[target]) - 1);
return lookup_[target][dist(gen_)];
}
private:
unordered_map<int, vector<int>> lookup_;
default_random_engine gen_;
};
// Time: ctor: O(1)
// pick: O(n)
// Space: O(1)
class Solution_TLE {
public:
Solution_TLE(vector<int> nums) : nums_(nums), gen_(random_device()()) {
}
int pick(int target) {
auto reservoir = -1;
auto n = 0;
for (int i = 0; i < nums_.size(); ++i) {
if (nums_[i] != target) {
continue;
}
uniform_int_distribution<int> dist(0, n++);
if (dist(gen_) == 0) {
reservoir = i;
}
}
return reservoir;
}
private:
const vector<int> nums_;
default_random_engine gen_;
};