当前位置:网站首页>497. random points in non overlapping rectangles
497. random points in non overlapping rectangles
2022-06-10 04:18:00 【Biqiliang】
497. Random points in non overlapping rectangles 【 Medium question 】【 A daily topic 】
Ideas :【 Pool sampling 】
Write it in detail tomorrow , In the eye , I'm really not free today ~
Code :
class Solution {
int[][] recs;
Random random;
public Solution(int[][] rects) {
this.recs = rects;
this.random = new Random();
}
public int[] pick() {
//idx Record the subscript of the currently selected rectangle cur Record the number of points in the current rectangle curSum Record after adding the point in the current rectangle , How many points are there in total
int idx = 0,n = recs.length;
long cur = 0,curSum = 0;
// Every time we traverse a rectangle , That is, calculate the number of points in the current rectangle cur, Then calculate how many points there are in total curSum
// Randomly select an integer point covered by a rectangle , So to ensure fairness , The probability of extracting the midpoint of the rectangle currently traversed should be cur/curSum
for (int i = 0; i < n; i++) {
int x1 = recs[i][0],y1 = recs[i][1],x2 = recs[i][2],y2 = recs[i][3];
cur = (long) (x2 - x1 + 1) * (y2 - y1 + 1);
curSum += cur;
if (random.nextLong(curSum + 1) < cur){
idx = i;
}
}
int x1 = recs[idx][0],y1 = recs[idx][1], x2 = recs[idx][2],y2 = recs[idx][3];
return new int[]{
x1 + random.nextInt(x2 - x1 + 1),y1 + random.nextInt(y2 - y1 + 1)};
}
}
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(rects); * int[] param_1 = obj.pick(); */
边栏推荐
- Pytorch CPU/GPU 安装方法。
- [understanding of opportunity -16]: Pyramid hierarchy conforms to nature
- MySQL learning
- [learn FPGA programming from scratch -14]: quick start chapter - operation step 3 (functional simulation) -3-modelsim quick start (8bits cycle counter)
- Eight part essay ceiling! (PDF HD download)
- Gevent | 异步就用它!
- On the night of the joint commissioning, I beat up my colleagues
- Distributed transaction solution
- How to view Scala source code in idea
- 作为软件测试工程师,给年轻时的自己的建议(上)
猜你喜欢

5分钟学会本地Pypi源搭建

这些编程语言老了,差不多死透了,年轻人不能碰

After working for 8 years, the monthly salary was 8000, but it didn't rise for 4 years. I got 15000 for job hopping. The former boss couldn't recruit anyone and begged to go back

3- programming language -- golang website learning

Qpprogressbar+qpushbutton+qmainwindow+qtmer+ layout manager +qtextcodec+qtexttospeech in QT

idea中怎樣編寫Scala代碼

图解固件、驱动、软件的区别

Open vs code from current folder
![[science and technology specialty-3]: how many students with science and technology specialty know about the senior high school entrance examination](/img/a6/f0a2f8dafffa28d2b2d2cd0431b739.jpg)
[science and technology specialty-3]: how many students with science and technology specialty know about the senior high school entrance examination

4-mirror address
随机推荐
[image fusion] Application of two-dimensional multi empirical mode decomposition in multi-scale image fusion with matlab code
As a software testing engineer, give advice to young people (Part 1)
Lit(一):创建组件
Difference and idempotency analysis between put and patch in rest API
5-常用工具管理
Comment écrire du Code Scala dans idea
Log management of MySQL database
[in depth study of 4g/5g/6g topic-23]: 5g NR startup process 4.1 - scheduling of the first uplink channel between MSG1 and PRACH and detection of time advance ta
90. 闭锁
[in depth study of 4g/5g/6g topic -29]: 5g NR startup process 5.1 - NR network architecture, core network configuration of base stations
EasyRecovery数据恢复软件100%恢复的成功率
[深入研究4G/5G/6G专题-26]: 5G NR开机流程4.4 - RRC连接应答消息MSG4(RRCSetup授权)的调度与消息内容详解
[机缘参悟-19]:鬼谷子-捭阖篇-阴阳即矛盾,是万事万物的基本属性,也是鬼谷子哲学的根基之一
tensorflow 中的 cross_entropy
为什么使用三层交换机
Online text string batch replacement tool
外观设计产品用途
Google Earth engine (GEE) - gpwv411: data set of average administrative unit area
[Error] anonymous type with no linkage used to declare function ‘bool InitSLinkList
FastApi-17-页面美化-2