当前位置:网站首页>【497. 非重叠矩形中的随机点】
【497. 非重叠矩形中的随机点】
2022-06-10 09:50:00 【Sugar_wolf】
来源:力扣(LeetCode)
描述:
给定一个由非重叠的轴对齐矩形的数组 rects ,其中 rects[i] = [ai, bi, xi, yi] 表示 (ai, bi) 是第 i 个矩形的左下角点,(xi, yi) 是第 i 个矩形的右上角角点。设计一个算法来随机挑选一个被某一矩形覆盖的整数点。矩形周长上的点也算做是被矩形覆盖。所有满足要求的点必须等概率被返回。
在一个给定的矩形覆盖的空间内任何整数点都有可能被返回。
请注意 ,整数点是具有整数坐标的点。
实现 Solution 类:
Solution(int[][] rects)用给定的矩形数组rects初始化对象。int[] pick()返回一个随机的整数点[u, v]在给定的矩形所覆盖的空间内。
示例 1:
输入:
["Solution","pick","pick","pick","pick","pick"]
[[[[-2,-2,-1,-1],[1,0,3,0]]],[],[],[],[],[]]
输出:
[null,[-1,-2],[2,0],[-2,-1],[3,0],[-2,-2]
解释:
Solution solution = new Solution([[-2, -2, 1, 1], [2, 2, 4, 6]]);
solution.pick(); // 返回 [1, -2]
solution.pick(); // 返回 [1, -1]
solution.pick(); // 返回 [-1, -2]
solution.pick(); // 返回 [-2, -2]
solution.pick(); // 返回 [0, 0]
提示:
1 <= rects.length <= 100rects[i].length == 4- -109 <= ai < xi <= 109
- -109 <= bi < yi <= 109
xi - ai <= 2000yi - bi <= 2000- 所有的矩形不重叠。
pick最多被调用 104 次。
方法:前缀和 + 二分查找
思路

代码:
class Solution {
public:
Solution(vector<vector<int>>& rects) : rects{
rects} {
this->arr.emplace_back(0);
for (auto & rect : rects) {
this->arr.emplace_back(arr.back() + (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1));
}
}
vector<int> pick() {
uniform_int_distribution<int> dis(0, arr.back() - 1);
int k = dis(gen) % arr.back();
int rectIndex = upper_bound(arr.begin(), arr.end(), k) - arr.begin() - 1;
k = k - arr[rectIndex];
int a = rects[rectIndex][0], b = rects[rectIndex][1];
int y = rects[rectIndex][3];
int col = y - b + 1;
int da = k / col;
int db = k - col * da;
return {
a + da, b + db};
}
private:
vector<int> arr;
vector<vector<int>>& rects;
mt19937 gen{
random_device{
}()};
};
执行用时:84 ms, 在所有 C++ 提交中击败了40.07%的用户
内存消耗:65.6 MB,在所有 C++ 提交中击败了65.70%的用户
复杂度分析
时间复杂度:构造函数复杂度为 O(n),pick 函数复杂度为 O(logn),其中 n 为 rects 的长度。构造函数需要构造前缀和数组,pick 函数需要在前缀和数组内进行二分。
空间复杂度:构造函数复杂度为O(n),pick 函数复杂度为 O(1),其中 n 为rects 的长度。构造函数需要构造前缀和数组,pick 函数只需要使用常数空间。
边栏推荐
- [fishing artifact] UI library second to second lowcode tool - list part (II) small tool for maintaining JSON
- [Timm] an image model library based on pytorch
- 2022年金属非金属矿山提升机操作考试题库及答案
- Comparison between rancher and kubesphere
- 成都測試設備定制_單片機C語言之數據類型初步介紹
- 你的下一台电脑何必是电脑,探索不一样的远程操作
- Do you know all the wonderful functions of the vlookup function?
- The R language coin package is applied to permutation tests for independence problems, one-way ANOVA and approximate k-sample permutation tests on the same data set, and comparing whether the mean val
- Four simplest anti reverse circuits
- 金融风控实战——异常检测(一)
猜你喜欢

Only this is the most true reason why leaders promote you. The rest is nonsense!

Theory and application of image processing

微软再曝“丑闻”:在办公室看 VR 黄片,“HoloLens 之父”即将离职!

金融风控实战——异常检测(一)

论 T 级互动开发如何在我们手上发光发热

MONGOREPLAY 的“坑”

416. segmentation and subsets

四种最简单的防反接电路

基于STM32设计智能家居控制系统(OneNet)_2022

QChart笔记1:简单线性图LineSeries
随机推荐
Genius! Only use four integers to write a snake game!
基于STM32设计智能家居控制系统(OneNet)_2022
Why is your next computer a computer? Explore different remote operations
The new domestic golang framework is ready to be released. Come and have a look
Example 2 of lambda expression
R语言plotly可视化:plotly可视化堆叠stacked的直方图、自定义设置条形之间的间距(Stacked Histograms)
R language uses rpart package to build decision tree model, selects appropriate tree size (complexity) to check cptable contents of decision tree objects (tree size is defined by splitting times and p
Ce soir - là, j'ai battu mon collègue...
Lambda表达式例一
【蓝桥杯集训100题】scratch苹果熟了 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第13题
你的下一台电脑何必是电脑,探索不一样的远程操作
5G 聯通網管設計思路
[essay for college entrance examination season] I have something to say about the college entrance examination
Flutter:自定义单选按钮
Uncaught TypeError: Cannot read properties of undefined (reading ‘colspan‘)
Microsoft exposes another "scandal": watching VR porn in the office, "the father of hololens" is about to leave!
Requirements and business model analysis - Requirements 17- requirements management
Comparison between rancher and kubesphere
YOLOX: Exceeding YOLO Series in 2021
[Blue Bridge Cup training 100 questions] scratch apple is ripe blue bridge cup scratch competition special prediction programming question intensive training simulation exercise question 13