-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path497.cpp
More file actions
executable file
·42 lines (36 loc) · 1.07 KB
/
497.cpp
File metadata and controls
executable file
·42 lines (36 loc) · 1.07 KB
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
class Solution {
public:
vector<vector<int>> rects;
vector<int> acc_weights;
int area_total = 0;
Solution(vector<vector<int>>& rects) {
this->rects = rects;
for (auto rect : rects){
area_total += (rect[2]-rect[0] + 1) * (rect[3] - rect[1] + 1);
this->acc_weights.push_back(area_total);
}
}
vector<int> pick() {
int w = rand() % area_total;
int left = 0, right = acc_weights.size(), mid;
while(left != right) {
mid = (left+right)/2;
if (w >= acc_weights[mid]) {
left = mid+1;
} else {
right = mid;
}
}
return pickRandomPoint(rects[left]);
}
vector<int> pickRandomPoint(vector<int> rect) {
int x = rand() % (rect[2] - rect[0] + 1);
int y = rand() % (rect[3] - rect[1] + 1);
return {rect[0] + x, rect[1] + y};
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(rects);
* vector<int> param_1 = obj->pick();
*/