-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomPoint.cpp
51 lines (40 loc) · 1.22 KB
/
randomPoint.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
class Solution {
public:
vector<int> total;
vector<vector<int>> rects;
Solution(vector<vector<int>>& rects) {
this->rects = rects;
int sum = 0;
for(vector<int> rect: rects) {
sum += (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1);
total.push_back(sum);
}
}
vector<int> pick() {
int maxValue = this->total[this->total.size() - 1];
int target = (rand() % maxValue);
int hi = total.size(), low = 0, mid;
while(low < hi) {
mid = low + (hi - low) / 2;
if(total[mid] > target) {
hi = mid;
} else {
low = mid + 1;
}
}
vector<int> rect = this->rects[low];
int x1 = rect[0];
int y1 = rect[1];
int x2 = rect[2];
int y2 = rect[3];
int x = (rand() % (x2 - x1 + 1));
int y = (rand() % (y2 - y1 + 1));
vector<int> pairs {x + x1, y + y1};
return pairs;
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(rects);
* vector<int> param_1 = obj->pick();
*/