当前位置:网站首页>LeetCode:497. 非重叠矩形中的随机点————中等
LeetCode:497. 非重叠矩形中的随机点————中等
2022-06-10 19:43:00 【Kinght_123】

题目
497. 非重叠矩形中的随机点
给定一个由非重叠的轴对齐矩形的数组 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], [2, 2, 4, 6]]], [], [], [], [], []]
输出:
[null, [1, -2], [1, -1], [-1, -2], [-2, -2], [0, 0]]
解释:
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 <= 100
- rects[i].length == 4
- -109 <= ai < xi <= 109
- -109 <= bi < yi <= 109
- xi - ai <= 2000
- yi - bi <= 2000
- 所有的矩形不重叠。
- pick 最多被调用 104 次。
解题思路
- 给定矩形的左小角(a,b),右上角(x,y),那么矩形内点的个数为(x-a+1)*(y-b+1)
- 我们可以从全部点的个数中随机一个数,从个数前缀和中确定是在第几个矩阵中。
- 再从该矩阵的宽或者高,确定随机的点在第几行第几个。
Code
class Solution:
def __init__(self, rects: List[List[int]]):
self.rects = rects
self.sum = [0]
for a, b, x, y in rects:
self.sum.append(self.sum[-1] + (x - a + 1) * (y - b + 1))
def pick(self) -> List[int]:
k = randrange(self.sum[-1])
rectIndex = bisect_right(self.sum, k) - 1
a, b, _, y = self.rects[rectIndex]
da, db = divmod(k - self.sum[rectIndex], y - b + 1)
return [a + da, b + db]
运行结果

边栏推荐
- 使用环绕通知对目标方法进行增强—摘抄笔记
- PDF. JS - - - - JS analyse le fichier PDF pour réaliser l'aperçu et obtenir le contenu du fichier PDF (sous forme de tableau)
- 服务管理与通信,基础原理分析
- Build a BPMN modeling Web Service
- 「Bug」问题分析 RuntimeError:which is output 0 of ReluBackward0
- Microsoft Word tutorial "5", how to change the margins and create a newsletter column in word?
- How to use the low code platform of the Internet of things for worksheet management?
- 观点丨Play and Earn 会让加密游戏误入歧途
- 获取列表中最大最小值的前n个数值的位置索引的四种方法
- How to realize face verification quickly and accurately?
猜你喜欢

【观察】昇腾智行:场景驱动,创新先行,为智慧交通按下“加速键”

移动端渲染原理浅析

How to use the low code platform of the Internet of things for worksheet management?

Service management and communication, basic principle analysis

获取列表中最大最小值的前n个数值的位置索引的四种方法
![[enter textbook latex record]](/img/f0/5ca60f0894d4ae544e7399d18a3a42.png)
[enter textbook latex record]

【录入课本latex记录】

vulnhub-The Planets: Earth

电子招标采购商城系统:优化传统采购业务,提速企业数字化升级

服务管理与通信,基础原理分析
随机推荐
torch.nn.Parameter的简单理解//未完待续,待我看懂这段
In depth learning experience and tools
传音 Infinix 新机现身谷歌产品库,TECNO CAMON 19 预装 Android 13
Service management and communication, basic principle analysis
堆叠条形图鼠标移入tooltip中提示过滤为0元素,实现自定义气泡
手写代码 bind
NFS network mount to create server image
H5 van-popup全屏弹窗,模拟页面回退效果,支持左上角返回按钮,适用物理返回,侧滑与底部返回键
Fs4100 lithium battery charging management IC input 12V to 8.4v charging IC
终于有人说清楚了Cookie、Session、Token的区别。详解,面试题。
观点丨Play and Earn 会让加密游戏误入歧途
农产品期货开户的条件是什么?现在开户的手续费是多少?
【技术碎片】重名文件 加后缀重命名过滤实现
中国工科研究生200多篇英文论文中最常见的习惯(The Most Common Habits from more than 200 English Papers written by Gradua)
Knife4j configuration can use direct copy
Analysis on rendering principle of mobile terminal
8.4v dual lithium battery professional charging IC (fs4062a)
CVPR 2022 Tsinghua University proposed unsupervised domain generalization (UDG)
搭建一个BPMN建模的Web服务
latex tips 绝对值的竖线 \left|\right|