当前位置:网站首页>leetcode:497. Random points in non overlapping rectangles [random + prefix and bisection + passing]
leetcode:497. Random points in non overlapping rectangles [random + prefix and bisection + passing]
2022-06-09 13:45:00 【Review of the white speed Dragon King】

analysis
First, record the number of points in each rectangle
Then make prefix and
When querying, generate a 1 Random number to all points
According to prefix and bisection, decide which rectangle to divide into
And then in this rectangle random Two coordinates are enough
ac code
class Solution:
def __init__(self, rects: List[List[int]]):
self.rects = rects
self.points = [(x - a + 1) * (y - b + 1) for a, b, x, y in rects]
self.preSum = list(accumulate(self.points))
self.tot = self.preSum[-1]
def pick(self) -> List[int]:
r = random.randint(1, self.tot)
idx = bisect_left(self.preSum, r)
a, b, x, y = self.rects[idx]
return self.helper(a, b, x, y)
def helper(self, a, b, x, y):
rx = random.randint(a, x)
ry = random.randint(b, y)
return [rx, ry]
# Your Solution object will be instantiated and called as such:
# obj = Solution(rects)
# param_1 = obj.pick()
summary
Random by chance
边栏推荐
- [leetcode weekly race record] record of the 79th biweekly race + the 295th weekly race
- 记一次程序内存占用高问题排查
- The curl post request carries the request header and passes the command to receive parameter data
- Take six five stars in Tencent (Part 2)
- 云呐|公司实物资产如何管理
- VMware ESXI software 英文版安装步骤
- 记录下bilibili(b站)小火箭页面上划动画效果的实现
- 5分钟了解NFV
- 面试题 05.08. 绘制直线
- “地球外存在生命之源”上热搜,外星发现氨基酸到底有什么用?
猜你喜欢

Yunna RFID asset management, advantages of RFID asset management system

郑州埃文科技安全头条

云呐|公司实物资产如何管理
![[C language practice - Young's matrix]](/img/4c/7599d962bce657c4d413e179b90b47.png)
[C language practice - Young's matrix]

手把手教你用js实现一个虚拟机

Yunna administrative unit fixed assets management system, unit fixed assets management measures

【C语言练习——打印整数二进制的奇数位和偶数位】

精益产品开发体系最佳实践及原则

数字化转型:如何获得组织的认可?

Database day-2
随机推荐
Yunna RFID asset management, advantages of RFID asset management system
[C language practice - adjust the order of odd and even numbers in the array]
炒作剽窃、内鬼欺诈 OpenSea上常见的NFT骗局及安全建议
2022.5.27-----leetcode.面试17.11
云呐|如何做好固定资产盘点?怎么盘点固定资产
云呐|服务器监控可视化工具
C language -- sequence stack
Teach you how to implement a virtual machine with JS
HCIA-Datacom实验 IPv4编址及IPv4路由基础实验
【C语言练习——合并两个有序序列】
Who says redis can't save big keys
Hype plagiarism, insider fraud common NFT scams and security suggestions on opensea
IDEA将现在新增加的修改合并cherry pick到之前的版本
Zhoubolei annual progress overview of model interpretability 20200805
Analysis on the resumption of the most serious downtime in the history of Facebook on October 4, 2021
How to do data visualization analysis
Yunna | how to manage fixed assets better? How to manage the company's fixed assets?
有损传输实例
斯坦福博士提出超快省显存Attention,GPT-2训练速度提升3.5倍,BERT速度创纪录
云呐|行政单位固定资产管理制度,单位固定资产管理办法