497. Random Point in Non-overlapping Rectangles
1. 问题
给定一系列不重叠的矩形,在这些矩形中随机采样一个整数点。
2. 思路
(1)一个矩形的可采样点个数就相当于它的面积,可以先依次对每个矩形的面积累加存起来(相当于概率分布中的分布累积函数CDF,Cumulative Distribution Function)。
(2)从 [1, 总面积] 中随机取一个数n(面积),表示要取第几个点,找到这个点即可完成题目要求的随机采样。
(3)找点可以先使用python的bisect_left方法,根据这个数(面积)在多个累加面积之间寻找合适的位置(第几个矩形)。然后根据这个数(面积)在那个矩形里找到这个点。
(4)bisect_left的作用是对已排序数组,查找目标数值将会插入的位置并返回(但是不会插入),数值相同时返回靠左的位置。
init:时间复杂度O(n),空间复杂度O(n)
pick:时间复杂度O(n),空间复杂度O(1)
3. 代码
import random
import bisect
class Solution(object):
def __init__(self, rects):
"""
:type rects: List[List[int]]
"""
self.rects = rects
sums = 0
self.accumulate = []
for x1, y1, x2, y2 in rects:
sums += (y2 - y1 + 1) * (x2 - x1 + 1)
self.accumulate.append(sums)
def pick(self):
"""
:rtype: List[int]
"""
n = random.randint(1, self.accumulate[-1])
i = bisect.bisect_left(self.accumulate, n)
x1, y1, x2, y2 = self.rects[i]
if(i > 0):
n -= self.accumulate[i - 1]
n -= 1
return [x1 + n % (x2 - x1 + 1), y1 + n / (x2 - x1 + 1)]
# Your Solution object will be instantiated and called as such:
# obj = Solution(rects)
# param_1 = obj.pick()
4. 类似题目
497. Random Point in Non-overlapping Rectangles的更多相关文章
- 【LeetCode】497. Random Point in Non-overlapping Rectangles 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/random-p ...
- Overlapping rectangles判断两个矩形是否重叠的问题 C++
Given two rectangles, find if the given two rectangles overlap or not. A rectangle is denoted by pro ...
- Determine overlapping rectangles
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- 2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)
There are nnn rectangles on the plane. The problem is to find the area of the union of these rectang ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
There are nn rectangles on the plane. The problem is to find the area of the union of these rectangl ...
- 2017 ACM/ICPC 南宁区 网络赛 Overlapping Rectangles
2017-09-24 20:11:21 writer:pprp 找到的大神的代码,直接过了 采用了扫描线+线段树的算法,先码了,作为模板也不错啊 题目链接:https://nanti.jisuanke ...
- Codeforces Round #497 (Div. 2)B. Turn the Rectangles
Bryce1010模板 http://codeforces.com/contest/1008/problems #include <bits/stdc++.h> using namespa ...
- 计蒜客 Overlapping Rectangles (离散化)
题意: 给定一个坐标系, 给出n个矩形的左下角坐标(bx,by)和右上角坐标(tx,ty) , 求矩形覆盖的面积, 有些区域会被多个矩形覆盖, 但只用算一次. n <= 1000, 0 < ...
- 528. Random Pick with Weight
1. 问题 给定一个权重数组w,w[i]表示下标i的权重,根据权重从数组中随机抽取下标. 2. 思路 这道题相当于 497. Random Point in Non-overlapping Recta ...
随机推荐
- 查看网卡流量:nload
nload命令用于查看网卡流量,用法如下: [root@localhost ~]$ yum install -y epel-release [root@localhost ~]$ yum instal ...
- JS面向对象编程学习
学习目标:1.掌握JS中的类(原型对象)和对象.2.什么是成员变量和成员方法.3.掌握构造方法的使用.补充:关于双等号(==):1.如果等号两边都是字符串时,则比较内容是否相等2.如果等号两边是数字时 ...
- WinForm软件开机自动启动详细方法
现在正在制作一个物资公司的管理软件,把自己掌握的学到的一点点细细的讲给喜欢C#的同仁们,互相交流. 想要给你制作的应用程序做一个开机启动,很方便,你可以让用户选择,在你的工具栏中的某个下拉菜单里添加一 ...
- 启动Solr时报 _version_ field must exist in schema 错误的解决方法
Solr启动时报 org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Unable to use up ...
- http://blog.csdn.net/ym19860303/article/details/8462450
http://www.himigame.com/iphone-cocos2d/444.html 很多游戏中,便于开发就会直接采用Cocos2d提供的字体库来使用,但是因为提供的种类很多,那么Himi利 ...
- c# Winform间的页面传值
Form2 public partial class Form2 : Form { public string str; public Form2() { InitializeComponent(); ...
- 在线预览文档(支持word、excel、ppt、pdf)+在线预览文档html版(转)
1.首先上网搜索一下有什么解决方案 (1).将文档转换为html,只支持支持office文档 (2).将文档转换为flash,实现类似百度文库的效果,除支持office文档外还支持pdf (1) a. ...
- SQL中 decode()函数简介(转载)
今天看别人的SQL时看这里面还有decode()函数,以前从来没接触到,上网查了一下,还挺好用的一个函数,写下来希望对朋友们有帮助哈! decode()函数简介: 主要作用:将查询结果翻译成其他值(即 ...
- supervisor安装篇
服务器环境: [root@kafka60 supervisor-]# uname -a Linux kafka60 -.el6.x86_64 # SMP Tue May :: UTC x86_64 x ...
- pandas的drop函数
当你要删除某一行或者某一列时,用drop函数,它不改变原有的df中的数据,而是返回另一个dataframe来存放删除后的数据. 1.命令: df.drop() 删除行:df.drop('apps') ...