497. Random Point in Non-overlapping Rectangles

https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/

题解

大坑:面积包含边界,计算要注意+1,这种随机数的问题,没有固定的输出,调bug不好调啊…目前的方法是,手动指定random的值,遍历所有可能的random,看是否符合预期。

思路:
1、先按照面积计算每个rectangle的权重
2、根据权重选择rectangle的下标(参考 528. Random Pick with Weight)
3、选好rectangle后,根据范围随机生成横坐标x、纵坐标y

class Solution {Random r;int N;int[][] rects;long[] weight; // 权重long[] weightSum; // 权重前缀和public Solution(int[][] rects) {this.rects = rects;r = new Random();N = rects.length;weight = new long[N];weightSum = new long[N];for (int i = 0; i < N; i++) {weight[i] = ((long) rects[i][2] + 1 - rects[i][0]) * ((long) rects[i][3] + 1 - rects[i][1]); // +1 因为包含边界!坑!!}weightSum[0] = 0;if (N > 1) weightSum[1] = weight[0];for (int i = 2; i < N; i++) {weightSum[i] = weightSum[i - 1] + weight[i - 1];}}public int[] pick() {// 根据权重选择一个rectanglelong sum = weight[N - 1] + weightSum[N - 1];long rand = randLong(0, sum);// 反向查找randLong属于的indexint L = 0;int R = N - 1;int index = -1;if (weightSum[N - 1] <= rand) {index = N - 1;} else {while (L < R) {int M = L + (R - L) / 2;if (weightSum[M] == rand) {index = M;break;} else if (weightSum[M] < rand) {if (L == M) {index = M;break;}L = M;} else {R = M - 1;if (weightSum[R] <= rand) {index = R;break;}}}if (index == -1) index = L;}int[] rec = rects[index];long x = randLong(rec[0], rec[2] + 1);long y = randLong(rec[1], rec[3] + 1);return new int[]{(int) x, (int) y};}public long randLong(long a, long b) { // 左闭右开return a + (long) (Math.random() * (b - a));}}/*** Your Solution object will be instantiated and called as such:* Solution obj = new Solution(rects);* int[] param_1 = obj.pick();*/

528. Random Pick with Weight

https://leetcode.com/problems/random-pick-with-weight/

本题是上一题的一个子问题,直接复制粘贴了。

class Solution {int N;int[] w; // 权重int[] wSum; // 权重前缀和public Solution(int[] w) {N = w.length;this.w = w;wSum = new int[N];wSum[0] = 0;if (N > 1) wSum[1] = this.w[0];for (int i = 2; i < N; i++) {wSum[i] = wSum[i - 1] + this.w[i - 1];}}public int pickIndex() {// 根据权重选择一个rectangleint sum = w[N - 1] + wSum[N - 1];int rand = rand(0, sum);// 反向查找randLong属于的indexint L = 0;int R = N - 1;int index = -1;if (wSum[N - 1] <= rand) {index = N - 1;} else {while (L < R) {int M = L + (R - L) / 2;if (wSum[M] == rand) {index = M;break;} else if (wSum[M] < rand) {if (L == M) {index = M;break;}L = M;} else {R = M - 1;if (wSum[R] <= rand) {index = R;break;}}}if (index == -1) index = L;}return index;}public int rand(int a, int b) { // 左闭右开return (int) (a + (Math.random() * (b - a)));}
}/*** Your Solution object will be instantiated and called as such:* Solution obj = new Solution(w);* int param_1 = obj.pickIndex();*/

leetcode 497, 528. Random Point in Non-overlapping Rectangles | 497. 非重叠矩形中的随机点(Java)相关推荐

  1. 【宫水三叶的刷题日记】497. 非重叠矩形中的随机点(中等)

    题目描述 这是 LeetCode 上的 497. 非重叠矩形中的随机点 ,难度为 中等. Tag : 「前缀和」.「二分」.「随机化」 给定一个由非重叠的轴对齐矩形的数组 rects,其中 表示 是第 ...

  2. LeetCode 497. 非重叠矩形中的随机点(前缀和+二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 给定一个非重叠轴对齐矩形的列表 rects,写一个函数 pick 随机均匀地选取矩形覆盖的空间中的整数点. 提示: 整数点是具有整数坐标的点. 矩形周边上 ...

  3. 【LeetCode】528. Random Pick with Weight 解题报告(Python)

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

  4. Random类(Java中的随机类)

    http://hi.baidu.com/jfeng_chan/item/3e4914b781a2db9e1846977b 在实际的项目开发过程中,经常需要产生一些随机数值,例如网站登录中的校验数字等, ...

  5. 《LeetCode力扣练习》剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 Java

    <LeetCode力扣练习>剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 Java 一.资源 题目: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数在数组 ...

  6. 《LeetCode力扣练习》第448题 找到所有数组中消失的数字 Java

    <LeetCode力扣练习>第448题 找到所有数组中消失的数字 Java 一.资源 题目: 给你一个含 n 个整数的数组 nums ,其中 nums[i] 在区间 [1, n] 内.请你 ...

  7. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...

  8. LeetCode 1031. 两个非重叠子数组的最大和(一次遍历,要复习)*

    文章目录 1. 题目 2. 解题 2.1 暴力枚举 2.2 一次遍历 1. 题目 给出非负整数数组 A ,返回两个非重叠(连续)子数组中元素的最大和,子数组的长度分别为 L 和 M.(这里需要澄清的是 ...

  9. Overlapping Experiment Infrastructure,重叠(分层)实验架构。

    重叠实验设施:更多.更好.更快地实验 1. 序言 2. 相关工作成果[略] 3. 背景 4. 重叠实验基础设施 5. 工具与流程 5.1 工具 5.2 实验设计与样本量 5.2.1 样本量 5.2.2 ...

最新文章

  1. DEV开发之控件NavBarControl
  2. 分布式电子邮件系统设计--转载
  3. “策小编数洞”开工啦,欢迎来唠两块钱儿的
  4. MySQL搭建主从复制 读写分离 分库分表 MyCat高可用
  5. Java 9 尝鲜之JShell交互式编程环境
  6. 网上看到的一道题,分享一下
  7. oracle 的使用
  8. kotlin 扩展类的功能_Kotlin程序| 扩展功能功能
  9. 黑盒攻击很难?元学习提高黑盒对抗攻击成功率
  10. 第三方网站调用微信公众号的图片被禁止
  11. 系统分析与设计课程总结
  12. 【C】VC6调试器的使用
  13. python并行编程手册 pdf_Python并行编程手册
  14. 麻瓜python视频教程_麻瓜编程Python Web基础视频教程
  15. 关于protel99和99se的系统字体设置问题
  16. Java单例模式简单代码
  17. oracle adjusting parallel,ora-29740故障求救
  18. 如何微信分享网页链接自定义图片和文字描述?生成微信自定义卡片链接流程(附教程与工具)
  19. Android WiFi 打开关闭流程
  20. 知识经济时代的基石:知识协同

热门文章

  1. 牛客 - 四等分的角度(几何)
  2. 中石油训练赛 - 腿部挂件(可持久化字典树)
  3. python 元组捷豹_GitHub - jaguarzls/pyecharts: Python Echarts Plotting Library
  4. 让软件不在添加删除程序_功能强大却鲜为人知的四款软件,一但发现就无法自拔...
  5. 数据分析机器学习-分类好坏的评价方式
  6. docker容器内无法下载到alpine的资源,报错network error (check Internet connection and firewall)
  7. VC程序初始化隐藏窗体
  8. [译]BitTorrent协议规范
  9. 用Python实现二叉树,完全二叉树和满二叉树
  10. 你知道SQL的这些错误用法吗?