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. 类似题目

528. Random Pick with Weight

497. Random Point in Non-overlapping Rectangles的更多相关文章

  1. 【LeetCode】497. Random Point in Non-overlapping Rectangles 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/random-p ...

  2. Overlapping rectangles判断两个矩形是否重叠的问题 C++

    Given two rectangles, find if the given two rectangles overlap or not. A rectangle is denoted by pro ...

  3. Determine overlapping rectangles

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

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

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

  5. 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 ...

  6. 2017 ACM/ICPC 南宁区 网络赛 Overlapping Rectangles

    2017-09-24 20:11:21 writer:pprp 找到的大神的代码,直接过了 采用了扫描线+线段树的算法,先码了,作为模板也不错啊 题目链接:https://nanti.jisuanke ...

  7. Codeforces Round #497 (Div. 2)B. Turn the Rectangles

    Bryce1010模板 http://codeforces.com/contest/1008/problems #include <bits/stdc++.h> using namespa ...

  8. 计蒜客 Overlapping Rectangles (离散化)

    题意: 给定一个坐标系, 给出n个矩形的左下角坐标(bx,by)和右上角坐标(tx,ty) , 求矩形覆盖的面积, 有些区域会被多个矩形覆盖, 但只用算一次. n <= 1000,  0 < ...

  9. 528. Random Pick with Weight

    1. 问题 给定一个权重数组w,w[i]表示下标i的权重,根据权重从数组中随机抽取下标. 2. 思路 这道题相当于 497. Random Point in Non-overlapping Recta ...

随机推荐

  1. Windows+IIS结合LVS+Keepalived是实现Linux负载均衡软件

    在Discuz!NT的最新版本(企业版)中,支持目前主流LINUX平台上的负载均衡解决方案,比如NGINX,HAPROXY,LVS等.本文与其说是解决方案,倒不如说是介绍如何搭建Discuz!NT负载 ...

  2. 佛祖保佑永无bug

    世界最难懂C语言代码竞赛: // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\_ ...

  3. js array.filter实例(数组去重)

    语法: 循环对数组中的元素调用callback函数, 如果返回true 保留,如果返回false 过滤掉,  返回新数组,老数组不变 var new_array = source_array.filt ...

  4. PyQt4颜色对话框QColorDialog

    QColorDialog提供了用于显示颜色的对话框. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import Qt ...

  5. qq邮箱发送,mail from address must be same as authorization user

    由于邮箱发送的邮箱账号更换,所以重新测试.结果一直出错,要不就是请求超时,要不就是未授权. 用smtp 开始的时候,端口使用495,结果是请求超时. 后来改成25,结果是未授权. 再后来听人说,有一个 ...

  6. Android之ListView分页数据加载

    1.效果如下: 实例如下:  上图的添加数据按钮可以换成一个进度条  因为没有数据所以我加了一个按钮添加到数据库用于测试:一般在服务器拉去数据需要一定的时间,所以可以弄个进度条来提示用户: 点击加载按 ...

  7. 【原创】asp.net内部原理(三) 第三个版本 (最详细的版本)

    前言: 今天继续吧这个系列补齐,这几天公司的项目比较忙,回到家已经非常的累了,所以也没顾得上天天来这里分享一些东西和大家一起探讨,但是今天晚上我还是坚持打开电脑,分享一些asp.net生命周期的知识, ...

  8. ios 监听设备旋转方向

    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { if(fromI ...

  9. IntelliJ Idea 2018 注册码

    转自:http://idea.lanyus.com/ *.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀 请搭建自己的IntelliJ IDEA授权服务 ...

  10. angularJs初体验,实现双向数据绑定!使用体会:比较爽

    使用初体验:ng 双向数据绑定: 最简单的双向数据绑定:(使用默认模块控制) <body ng-app> <input type="text" ng-model= ...