Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly and uniformily picks an integer point in the space covered by the rectangles.

Note:

  1. An integer point is a point that has integer coordinates.
  2. A point on the perimeter of a rectangle is included in the space covered by the rectangles.
  3. ith rectangle = rects[i] = [x1,y1,x2,y2], where [x1, y1] are the integer coordinates of the bottom-left corner, and [x2, y2] are the integer coordinates of the top-right corner.
  4. length and width of each rectangle does not exceed 2000.
  5. 1 <= rects.length <= 100
  6. pick return a point as an array of integer coordinates [p_x, p_y]
  7. pick is called at most 10000 times.

Example 1:

Input:
["Solution","pick","pick","pick"]
[[[[1,1,5,5]]],[],[],[]]
Output:
[null,[4,1],[4,1],[3,3]]

Example 2:

Input:
["Solution","pick","pick","pick","pick","pick"]
[[[[-2,-2,-1,-1],[1,0,3,0]]],[],[],[],[],[]]
Output:
[null,[-1,-2],[2,0],[-2,-1],[3,0],[-2,-2]]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution's constructor has one argument, the array of rectangles rectspick has no arguments. Arguments are always wrapped with a list, even if there aren't any.

这道题给了我们一些非重叠的矩形,让我们返回一个这些矩形中的一个随机的点。那么博主的第一直觉就是首先要在这些矩形中随机挑出来一个,然后在这个随机的矩形中再随机生成一个点,通过随机生成一个长和宽即可。博主最开始想到的方法是用rand随机生成一个 [0, n) 范围内的数字,n为输入矩形的个数,这样就得到了一个随机的矩形。但是这种方法貌似行不通,会跪在一个很长的输入测试数据。这使得博主比较困惑了,没有想出原因是为何,有哪位看官大神们知道的,麻烦留言告知博主哈!哈,已经知道了,参见评论区二楼留言~ 论坛上的解法有一种是用水塘抽样Reservoir Sampling的方法的,LeetCode之前有过几道需要用这种方法的题目 Random Pick IndexShuffle an Array 和 Linked List Random Node。这里我们使用其来随机出一个矩形,做法是遍历所有的矩形,用变量sumArea来计算当前遍历过的所有矩形面积之和,然后变量area是当前遍历的矩形的面积(注意这里不是严格意义上的面积,而是该区域内整数坐标的点的个数,所以长宽相乘的时候都要加1),然后我们在当前所有矩形面积之和内随机生成一个值,如果这个值小于area,那么选择当前的矩阵为随机矩形。这里相当于一个大小为area的水塘,在这个值之内的话,就更换selected。这个方法是没啥问题,但是博主还是没想通为啥不能直接随机生成矩形的index。当我们拿到随机矩形后,之后就随机出宽和高返回即可,参见代码如下:

解法一:

class Solution {
public:
Solution(vector<vector<int>> rects) {
_rects = rects;
} vector<int> pick() {
int sumArea = ;
vector<int> selected;
for (auto rect : _rects) {
int area = (rect[] - rect[] + ) * (rect[] - rect[] + );
sumArea += area;
if (rand() % sumArea < area) selected = rect;
}
int x = rand() % (selected[] - selected[] + ) + selected[];
int y = rand() % (selected[] - selected[] + ) + selected[];
return {x, y};
} private:
vector<vector<int>> _rects;
};

这道题在论坛上的主流解法其实是这个,我们用TreeMap来建立当前遍历过的矩形面积之和跟该矩形位置之间的映射。然后当我们求出所有的矩形面积之和后,我们随机生成一个值,然后在TreeMap中找到第一个大于这个值的矩形,这里博主还是有疑问,为啥不能直接随机矩形的位置,而是非要跟面积扯上关系。之后的步骤就跟上面的没啥区别了,参见代码如下:

解法二:

class Solution {
public:
Solution(vector<vector<int>> rects) {
_rects = rects;
_totalArea = ;
for (auto rect : rects) {
_totalArea += (rect[] - rect[] + ) * (rect[] - rect[] + );
_areaToIdx.insert({_totalArea, _areaToIdx.size()});
}
} vector<int> pick() {
int val = rand() % _totalArea;
int idx = _areaToIdx.upper_bound(val)->second;
int width = _rects[idx][] - _rects[idx][] + ;
int height = _rects[idx][] - _rects[idx][] + ;
return {rand() % width + _rects[idx][], rand() % height + _rects[idx][]};
} private:
vector<vector<int>> _rects;
int _totalArea;
map<int, int> _areaToIdx;
};

类似题目:

Random Pick with Weight

Generate Random Point in a Circle

参考资料:

https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/

https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/discuss/155005/C%2B%2B-single-rand()-call

https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/discuss/169185/Short-C%2B%2B-solution-with-upper_bound

https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/discuss/170503/C%2B%2B-solution-using-reservoir-sampling-with-explanation-concise-and-easy-to-understand

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Random Point in Non-overlapping Rectangles 非重叠矩形中的随机点的更多相关文章

  1. [Swift]LeetCode497. 非重叠矩形中的随机点 | Random Point in Non-overlapping Rectangles

    Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly ...

  2. 2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)

    There are nnn rectangles on the plane. The problem is to find the area of the union of these rectang ...

  3. [Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays

    Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping ...

  4. 在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数

    目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造 ...

  5. Android 高级UI设计笔记17:Android在非UI线程中显示Toast

    1. 子线程的Toast怎么显示不出来? 因为Toast在创建的时候会依赖于一个Handler,并且一个Handler是需要有一个Looper才能够创建,而普通的线程是不会自动去创建一个Looper对 ...

  6. leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  7. 在非MFC程序中引用CString

    CString在当今软件设计界里还是小有名气的,说它是MFC中使用的最多的类一点也不过,然而在使用sdk编windows程序的时候,确不能利用CString类,只能用sdk的运行时库,比如strlen ...

  8. 如何在非 React 项目中使用 Redux

    本文作者:胡子大哈 原文链接:https://scriptoj.com/topic/178/如何在非-react-项目中使用-redux 转载请注明出处,保留原文链接和作者信息. 目录 1.前言 2. ...

  9. 关于C#中”扩展方法必须在非泛型静态类中定义“问题的解决

    问题描述: 在某个窗口下的编码中使用了以下扩展方法FindControl,以求根据字符串的值去操作控件(本文中的控件为Label控件)的属性. public static Control FindCo ...

随机推荐

  1. 第三节:Action向View传值的四种方式(ViewData、ViewBag、TempData、Model)

    简  介 在前面的章节中,我们已经很清楚,MVC工作模型的流程,Controller中的Action接收到客户端的请求,处理后要将数据返回给View,那么Action中是如何将数据返回给View的,二 ...

  2. Element老司机开车了

    orm Select选择器   坑: 1.选择器视图层一直渲染最后一个元素(value-key作为唯一标识符是关键)2.视图不更新,没有在data函数中声明变量,或者其他地方重置了已经声明过得变量 o ...

  3. Notepad++崩溃后文件内容找不到问题

    也许是因为Ctrl + s 摁太多太频繁,一不小心Notepad++崩溃了 重启后发现原来的文件还在,但是文件内容全部都被清空了 我没有手动备份这个文件, 如何找回??? 点击设置,首选项,里面有个备 ...

  4. 让iframe自适应高度-真正解决

    需求:实现 iframe 的自适应高度,能够随着页面的长度自动的适应以免除页面和 iframe 同时出现滚动条的现象. (需要只有iframe出现滚动条) 本人一开始这么写:会造成只有主页面加载是设定 ...

  5. blinker库

    参考 Blinker Documentation Blinker 是一个基于Python的强大的信号库,它既支持简单的对象到对象通信,也支持针对多个对象进行组播.Flask的信号机制就是基于它建立的. ...

  6. Linux shell 脚本总结

    在中括号中,判断变量的值, 加不加双引号的问题? -z 判断 变量的值,是否为空: zero = 0 - 变量的值,为空,返回0,为true - 变量的值,非空,返回1,为false -n 判断变量的 ...

  7. linux 批量测试域名返回码脚本

    需求:应用要求覆盖host并测试 1.创建一个host.txt的文件来存放需要修改的host记录 2.过滤出host.txt域名列并新生成一个curl.txt文件 cat host.txt |awk ...

  8. rsyslog磁盘辅助(Disk-Assisted)模式踩坑记

    最近公司为方便tracing.排查, 搞全链路日志收集,而我手上的10亿+pv的动态前端服务必然在考虑之列. 之前呢. 都是运维定制的收集方式: 如上图,rsyslog push kafka, 优点嘛 ...

  9. Java_变量类型

    目录 主要是为了复习java相关知识,本文主要内容来自于 http://www.runoob.com/java 一.局部变量 局部变量声明在方法.构造方法或语句块中 在方法.构造方法.语句块被执行的时 ...

  10. hdu4507 数位dp+推公式

    推公式的能力需要锻炼.. /* dp的时候要存结构体 里面三个元素: cnt,就是满足条件的个数 sum1,就是满足条件的数字和 sum2,满足条件的数字平方和 推导过程:还是用记忆化搜索模板 dp[ ...