forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
68 lines (49 loc) · 1.71 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
61
62
63
64
65
66
67
68
/// Source : https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/description/
/// Author : liuyubobobo
/// Time : 2018-08-28
#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
using namespace std;
/// PreSum and Binary Search
/// Time Complexity: init: O(n)
/// pick: O(logn)
/// Space Complexity: O(n)
class Solution {
private:
vector<vector<int>> rects;
vector<int> counts;
public:
Solution(vector<vector<int>> rects): rects(rects.begin(), rects.end()) {
counts.clear();
counts.push_back(0);
for(const vector<int>& rect: rects)
counts.push_back(counts.back() + (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1));
// cout << "couts : "; Solution::print_vec(counts);
}
vector<int> pick() {
int randNum = rand() % counts.back();
int index = lower_bound(counts.begin(), counts.end(), randNum) - counts.begin();
if(counts[index] != randNum)
index --;
int offset = randNum - counts[index];
// int m = rects[index][3] - rects[index][1] + 1;
int n = rects[index][2] - rects[index][0] + 1;
// cout << "rectangle : "; Solution::print_vec(rects[index]);
// cout << "offset = " << offset << "; n = " << n << endl;
return {rects[index][0] + offset % n, rects[index][1] + offset / n};
}
static void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
};
int main() {
vector<vector<int>> rects = {{-2, -2, -1, -1}, {1, 0, 3, 0}};
Solution solution(rects);
for(int i = 0; i < 5; i ++)
Solution::print_vec(solution.pick());
return 0;
}