当前位置:网站首页>LeetCode:497. Random points in non overlapping rectangles -- medium
LeetCode:497. Random points in non overlapping rectangles -- medium
2022-06-10 20:49:00 【Kinght_ one hundred and twenty-three】

Catalog
subject
497. Random points in non overlapping rectangles
Given an array of rectangles aligned by non overlapping axes rects , among rects[i] = [ai, bi, xi, yi] Express (ai, bi) It's No i Bottom left corner of rectangle ,(xi, yi) It's No i The upper right corner of a rectangle . Design an algorithm to randomly select an integer point covered by a rectangle . Points on the perimeter of a rectangle are also considered to be covered by the rectangle . All points that meet the requirements must be returned with equal probability .
Any integer point in the space covered by a given rectangle can be returned .
Please note that , An integer point is a point with integer coordinates .
Realization Solution class :
Solution(int[][] rects) With the given rectangular array rects Initialize object .int[] pick() Returns a random integer point [u, v] In the space covered by a given rectangle .
Example 1:
Input :
[“Solution”, “pick”, “pick”, “pick”, “pick”, “pick”]
[[[[-2, -2, 1, 1], [2, 2, 4, 6]]], [], [], [], [], []]
Output :
[null, [1, -2], [1, -1], [-1, -2], [-2, -2], [0, 0]]
explain :
Solution solution = new Solution([[-2, -2, 1, 1], [2, 2, 4, 6]]);
solution.pick(); // return [1, -2]
solution.pick(); // return [1, -1]
solution.pick(); // return [-1, -2]
solution.pick(); // return [-2, -2]
solution.pick(); // return [0, 0]
Tips :
- 1 <= rects.length <= 100
- rects[i].length == 4
- -109 <= ai < xi <= 109
- -109 <= bi < yi <= 109
- xi - ai <= 2000
- yi - bi <= 2000
- All rectangles do not overlap .
- pick At most called 104 Time .
Their thinking
- The small left corner of a given rectangle (a,b), Upper right corner (x,y), Then the number of points in the rectangle is (x-a+1)*(y-b+1)
- We can choose a random number from all the points , From the sum of the number prefixes, it is determined in which matrix .
- Then from the width or height of the matrix , Determine the number of random points in the row .
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]
Running results

边栏推荐
- 自注意力(self-attention)和多头注意力(multi-head attention)
- C语言 浮点数 储存形式
- KCon 2022 议题大众评选火热进行中!不要错过“心仪”的议题哦~
- How to realize face verification quickly and accurately?
- Microsoft Word 教程「5」,如何在 Word 中更改頁邊距、創建新聞稿欄?
- RuntimeError: Attempting to deserialize object on CUDA device 1 but torch. cuda. device_ count() is 1.
- Cut rope / integer split
- 详解三级缓存解决循环依赖
- Build a BPMN modeling Web Service
- 请问九洲期货是正规的吗?开户安不安全
猜你喜欢

Hm3416h buck IC chip pwm/pfm controls DC-DC buck converter

堆叠条形图鼠标移入tooltip中提示过滤为0元素,实现自定义气泡

pytorch深度学习——卷积操作以及代码示例

聊聊服务器性能优化~(建议收藏)

2台电脑共享一套键盘鼠标

Jiangbolong forestee xp2000 PCIe 4.0 SSD multi encryption function, locking data security

Microsoft Word 教程「5」,如何在 Word 中更改页边距、创建新闻稿栏?
![JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation](/img/42/bcda46a9297a544b44fea31be3f686.png)
JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation

Tutoriel Microsoft Word "5", comment changer les marges de page et créer une barre de nouvelles en word?

synergy: server refused client with our name
随机推荐
Mysql database foundation
聊聊服务器性能优化~(建议收藏)
自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日
node(express)实现增删改查、分页等接口
An old programmer of about 10 years said: simple crud function enters the era of codeless development 1. Adding, deleting, modifying and checking interface information
农产品期货开户的条件是什么?现在开户的手续费是多少?
MySQL - common functions
Cloud native community boss blog
CVPR 2022丨清华大学提出:无监督域泛化 (UDG)
堆叠条形图鼠标移入tooltip中提示过滤为0元素,实现自定义气泡
服务管理与通信,基础原理分析
游戏兼容性测试(通用方案)
轻便型FDW框架 for pb
[Legendre] polynomial
The most common habits from more than 200 English papers written by gradua
Canvas advanced functions (Part 1)
江波龙 FORESEE XP2000 PCIe 4.0 SSD 多重加密功能,锁定数据安全
Mixin -- mixed
京东发布基于张量网络加速的大规模、分布式量子机器学习平台TeD-Q
mixin--混入