当前位置:网站首页>Daily question - leetcode497 - random points in non overlapping rectangle - prefix and - bisection
Daily question - leetcode497 - random points in non overlapping rectangle - prefix and - bisection
2022-06-10 05:09:00 【Li Fan, hurry up】
Note:
Larger area , The higher the probability of winning , Take a sample from a rectangle , Abscissa and ordinate independent , Just generate them randomly
But first determine which rectangle it is
In one dimension , Use a prefix and an array to record the area and
Generate one number at a time , It will fall into this one-dimensional array , Use bisection to find out which rectangle is the corresponding position
Prefixes and arrays are subscripts from 1 At the beginning , So the range of two points is 1 ~ n
Subtract one from the result of two Can be mapped to the matrix array of the given input
The code is as follows :
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(); */
边栏推荐
- openGauss数据库性能调优概述及实例分析
- S series · several postures for deleting folders
- Pytorch: sub model parameter freezing + BN freezing
- OpenJudge NOI 1.13 14:求满足条件的3位数
- 自定义Tooltips提示气泡Js插件
- Mindspire deletion and torch nn. Maxpool3d and torch nn. Avgpool3d benchmarking operator
- Read leastereo:hierarchical neural architecture search for deep stereo matching
- Powerful development board
- [STM32] Hal library -spi
- S系列·修改文件的时间属性
猜你喜欢

IDEA不小心排除常用类的自动导包或补全

Review of major events | quick view of important developments of eolink in May!

Literature reading -- location of regulatory variation in maize drought response and tolerance gene expression

一些漂亮的js提示框

第六章 软件测试工具(此章完结)

S series · time attribute of modified file

Proteus仿真stm32f103R6Tx——外部中断控制LED亮灭(Cube MX+Keil5+proteus)

Installing mindinsight in the mindspire official website container does not work locally

Contact QR code generation plug-in qrcode js

Read leastereo:hierarchical neural architecture search for deep stereo matching
随机推荐
2022山东省安全员C证考试题库及答案
Array, string, function and structure in C language
Why is mindspore 1.5rcgraph mode slow to train?
2022制冷与空调设备运行操作特种作业证考试题库模拟考试平台操作
【Linux篇<Day20>】——一文入门 数据库 和 容器技术
25. Bom Event
【无标题】
信息学奥赛一本通 1287:最低通行费 | OpenJudge NOI 2.6 7614:最低通行费
一些漂亮的js提示框
2022.6.1-----leetcode. four hundred and seventy-three
Distributed loading model and incremental training on shengteng 910
Interview question 08.07 Permutation without duplicate strings
得物登录组件重构
Question bank and answers of operation certificate examination for safety production management personnel of hazardous chemical production units in 2022
Custom tooltips prompt bubble JS plug-in
Nx logo from brushing to switching on
Use mindspire in gpu-national/ cpu-graph_ Mode and gpu-graph_ Inconsistent mode execution
JS wechat games - fighting mosquitoes
Powerful development board
Odometer. JS digital scrolling plug-in
