forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC0497.cpp
More file actions
executable file
·50 lines (45 loc) · 1.28 KB
/
LC0497.cpp
File metadata and controls
executable file
·50 lines (45 loc) · 1.28 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
43
44
45
46
47
48
49
50
/*
Problem Statement: https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
|-----------------|----------|-------|
| Operations | Time | Space |
|-----------------|----------|-------|
| Solution(rects) | O(n) | O(1) |
| pick() | O(log n) | O(1) |
|-----------------|----------|-------|
*/
class Solution {
private:
int area;
mt19937 gen;
vector<double> weights;
vector<vector<int>> rects;
public:
Solution(vector<vector<int>>& rects) : area(0), gen(random_device{}()), rects(rects) {
for (vector<int>& rect: rects) {
weights.push_back(get_area(rect));
area += weights.back();
}
double sum = 0;
for (double& weight: weights) {
sum += weight;
weight = sum / area;
}
}
int get_area(vector<int>& rect) {
return (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1);
}
int get_random(int mn, int mx) {
return uniform_int_distribution<int>{mn, mx}(gen);
}
vector<int> pick() {
int x, y, pos;
double prob = uniform_real_distribution<double>{0, 1}(gen);
pos = distance(weights.begin(), lower_bound(weights.begin(), weights.end(), prob));
vector<int>& rect = rects[pos];
x = get_random(rect[0], rect[2]);
y = get_random(rect[1], rect[3]);
return {x, y};
}
};