当前位置:网站首页>每日一题—LeetCode497—非重叠矩形中的随机点-前缀和-二分
每日一题—LeetCode497—非重叠矩形中的随机点-前缀和-二分
2022-06-10 05:03:00 【李烦烦搞快点】
Note:
面积越大,抽中的概率越高,从矩形中抽样好抽,横纵坐标独立的,都随机生成一下就行
但是要先确定是哪一个矩形
展成一维的情况,用一个前缀和数组来记录面积和
每次随机生成一个数,他会落到这个一维的数组中,用二分去查找落得对应位置是哪一个矩形
前缀和数组是下标从1开始的,那么二分的范围就是1 ~ n
二分出来的结果减一 才能对应到给的输入的矩阵数组里
代码如下:
class Solution {
public:
int n;
vector<vector<int>> rects;
vector<int> sum;
Solution(vector<vector<int>>& _rects) {
sum.push_back(0);
rects = _rects;
n = rects.size();
for(auto r: rects){
int w = r[2] - r[0] + 1, h = r[3] - r[1] + 1;
sum.push_back(sum.back() + w * h);
}
}
vector<int> pick() {
int k = rand() % sum.back() + 1;
int l = 1, r = n;
while(l < r){
int mid = l + r >> 1;
if(sum[mid] >= k) r = mid;
else l = mid + 1;
}
auto t = rects[r - 1];
int dx = t[2] - t[0] + 1, dy = t[3] - t[1] + 1;
return {
rand() % dx + t[0], rand() % dy + t[1]};
}
};
/** * Your Solution object will be instantiated and called as such: * Solution* obj = new Solution(rects); * vector<int> param_1 = obj->pick(); */
边栏推荐
- 2022.5.26-----leetcode. six hundred and ninety-nine
- 信息学奥赛一本通 1287:最低通行费 | OpenJudge NOI 2.6 7614:最低通行费
- Interview question 05.06 Integer conversion
- Distributed loading model and incremental training on shengteng 910
- photoClip. JS mobile image upload and interception plug-in
- Review of major events | quick view of important developments of eolink in May!
- MindSpore【初学入门】教程在线运行时报错
- Idea accidentally excludes automatic package import or completion of common classes
- Powerful development board
- Solution to control double click conflict in WPF
猜你喜欢

Odometer.js数字滚动插件

得物登录组件重构

Byte order, object class

2022 examination questions and online simulation examination for main principals of hazardous chemical business units

2022年危险化学品生产单位安全生产管理人员操作证考试题库及答案

2022年广东省安全员A证第三批(主要负责人)考试题及在线模拟考试

MindSpore【初学入门】教程在线运行时报错

25. BOM事件

大事件回顾 | Eolink 5月重要动态速览!

S series · several postures for deleting folders
随机推荐
Literature reading -- location of regulatory variation in maize drought response and tolerance gene expression
自定義Tooltips提示氣泡Js插件
S系列·修改文件的时间属性
2022.6.8-----leetcode. one thousand and thirty-seven
得物登录组件重构
Question bank and answers of operation certificate examination for safety production management personnel of hazardous chemical production units in 2022
S系列·删除文件夹的几种姿势
Softing provides Emerson with connection solutions for AMS device management system
Interview question 08.04 Power set
2022g1 industrial boiler stoker examination questions and answers
Briefly talk about the difference between routers and switches
Redis specifies the configuration file startup, database related instructions, and redis operation string and list types
Examination questions and online simulation examination of the third batch of Guangdong Provincial Safety Officer a certificate (principal) in 2022
js微信小游戏之打蚊子
【Linux篇<Day20>】——一文入门 数据库 和 容器技术
2022.6.5-----leetcode. four hundred and seventy-eight
Maximum difference between left and right maximum values
如何使用ODX描述诊断会话和安全等级
跳高微信js小游戏源码
Mindspire [dataset function] cannot view datasets
