-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
60 lines (52 loc) · 1.05 KB
/
main.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
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
private:
vector<int> nums;
public:
Solution(vector<int>& nums) {
this->nums = nums;
}
int pick(int target) {
int pos = -1;
int count = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == target) {
if (count > 0) {
count++;
int rand = (int)(std::rand() * RAND_MAX) % count;
if (rand == 1) {
pos = i;
}
} else {
pos = i;
count = 1;
}
}
}
return pos;
}
};
int main() {
vector<int> nums = {1, 2, 3, 3, 3};
Solution sol(nums);
cout << sol.pick(3)<< endl;
cout << sol.pick(1)<< endl;
cout << sol.pick(3)<< endl;
cout << sol.pick(3)<< endl;
cout << sol.pick(3)<< endl;
cout << sol.pick(2)<< endl;
cout << sol.pick(3)<< endl;
return 0;
}