日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

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

發(fā)布時(shí)間:2024/2/28 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 497, 528. Random Point in Non-overlapping Rectangles | 497. 非重叠矩形中的随机点(Java) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

497. Random Point in Non-overlapping Rectangles

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

題解

大坑:面積包含邊界,計(jì)算要注意+1,這種隨機(jī)數(shù)的問(wèn)題,沒(méi)有固定的輸出,調(diào)bug不好調(diào)啊…目前的方法是,手動(dòng)指定random的值,遍歷所有可能的random,看是否符合預(yù)期。

思路:
1、先按照面積計(jì)算每個(gè)rectangle的權(quán)重
2、根據(jù)權(quán)重選擇rectangle的下標(biāo)(參考 528. Random Pick with Weight)
3、選好rectangle后,根據(jù)范圍隨機(jī)生成橫坐標(biāo)x、縱坐標(biāo)y

class Solution {Random r;int N;int[][] rects;long[] weight; // 權(quán)重long[] weightSum; // 權(quán)重前綴和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 因?yàn)榘吔?#xff01;坑!!}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() {// 根據(jù)權(quán)重選擇一個(gè)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) { // 左閉右開(kāi)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/

本題是上一題的一個(gè)子問(wèn)題,直接復(fù)制粘貼了。

class Solution {int N;int[] w; // 權(quán)重int[] wSum; // 權(quán)重前綴和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() {// 根據(jù)權(quán)重選擇一個(gè)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) { // 左閉右開(kāi)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();*/

總結(jié)

以上是生活随笔為你收集整理的leetcode 497, 528. Random Point in Non-overlapping Rectangles | 497. 非重叠矩形中的随机点(Java)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。