Skip to content

Commit f08b71d

Browse files
committed
Updates leetcode/binary-search/497-random-point-in-non-overlapping-rectangles.md
Auto commit by GitBook Editor
1 parent 3a3435c commit f08b71d

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

leetcode/binary-search/497-random-point-in-non-overlapping-rectangles.md

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly and uniformily picks an integer point in the space covered by the rectangles.
66

7-
**Note:
7+
**Note:
88
**
99

1010
1. An integer point is a point that has integer coordinates.
@@ -45,51 +45,48 @@ The input is two lists: the subroutines called and their arguments. Solution's c
4545
1. To get a random point, we have to locate one rectangle then pick from it
4646
2. Initial thought using area fails due to some rectangles have area of 0, we use count of points instead
4747
3. As we count through each rectangles, we accumulate the count in presum array and record total counts
48-
4. Then we generate a number, r, in \[0, totalCount\), we perform binary search to locate the rectangle that contains it \(using number in \[1, totalCount\] will keep binary search traditional\)
48+
4. Then we generate a number, r, in \[0, total\) or better \[1, total\], this number is used to find the largest presum that are smaller or equal to r
4949
5. After locating the rectangle, we can either generate a point in x plus a point in y or use r generated above to get the final result
50-
6. The second way avoid extra random point generation, and the formula is x = x0 + \(r % c\), and y = y0 + \(r / c\) where r is reset to zero-based and rectangle-based by r - presum\[prev\] + 1 and c points count
50+
6. The second way avoid extra random point generation, and the formula is x = x0 + \(r % w\), and y = y0 + \(r / w\) where r is reset to zero-based and rectangle-based by presum\[i\] - r and w is width of rectangle
5151
7. Time complexity O\(n\)
5252
8. Space complexity O\(n\)
53-
2. asd
5453

5554
### Solution
5655

5756
```java
5857
class Solution {
59-
private int totalArea;
58+
private int total;
6059
private int[][] rects;
61-
private int[] a;
60+
private int[] presum;
6261
private Random rand;
6362

6463
public Solution(int[][] rects) {
65-
this.totalArea = 0;
64+
this.total = 0;
6665
this.rects = rects;
67-
this.a = new int[rects.length];
66+
this.presum = new int[rects.length];
6867
this.rand = new Random();
6968
for (int i = 0; i < rects.length; i++) {
70-
totalArea += getArea(rects[i]);
71-
a[i] = totalArea;
69+
total += (rects[i][2] - rects[i][0] + 1) * (rects[i][3] - rects[i][1] + 1) ;
70+
presum[i] = total;
7271
}
7372
}
74-
75-
private int getArea(int[] rect) {
76-
return (rect[2] - rect[0]) * (rect[3] - rect[1]);
77-
}
78-
73+
7974
public int[] pick() {
80-
int i = binarySearch(a, rand.nextInt(totalArea) + 1);
81-
int x = rand.nextInt(rects[i][2] - rects[i][0] + 1) + rects[i][0];
82-
int y = rand.nextInt(rects[i][3] - rects[i][1] + 1) + rects[i][1];
83-
return new int[] {x, y};
75+
int r = rand.nextInt(total) + 1;
76+
int i = binarySearch(presum, r);
77+
r = presum[i] - r;
78+
int w = rects[i][2] - rects[i][0] + 1;
79+
int x = rects[i][0] + r % w;
80+
int y = rects[i][1] + r / w;
81+
return new int[]{x, y};
8482
}
8583

8684
private int binarySearch(int[] nums, int target) {
8785
int lo = 0, hi = nums.length - 1;
88-
while (lo <= hi) {
86+
while (lo < hi) {
8987
int mi = lo + (hi - lo) / 2;
90-
if (nums[mi] == target) return mi;
91-
else if (nums[mi] < target) lo = mi + 1;
92-
else hi = mi - 1;
88+
if (target > nums[mi]) lo = mi + 1;
89+
else hi = mi;
9390
}
9491
return lo;
9592
}

0 commit comments

Comments
 (0)