Skip to content

Commit c1e61dc

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

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

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

Lines changed: 53 additions & 7 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.
@@ -43,17 +43,63 @@ The input is two lists: the subroutines called and their arguments. Solution's c
4343

4444
1. Binary Search
4545
1. Since the rectangles are non-overlapping, we can use each rectangle's area divided by total as probability of being picked
46-
2. We generate a random number, r, in the range of \[0, totalArea\)
47-
3. Then r is used to find out which rectangle is picked through process of binary search through accumulated area array
48-
4. After locating the rectangle, we can easily generate random point in this rectangle
49-
5. Time complexity O\(n\), where n is number of rectangles
50-
6. Space complexity O\(n\)
46+
2. Unfortunately some rectangles have zero area, therefore we need to use count of points instead
47+
3. In the process of counting, we accumulated the count and save it to presum array
48+
4. After generating r in the range of \[0, totalCount\), we use it to find out which rectangle is picked through process of binary search through presum array
49+
50+
1. After locating the rectangle, we can either generate
51+
2. Time complexity O\(n\), where n is number of rectangles
52+
3. Space complexity O\(n\)
5153
2. asd
5254

5355
### Solution
5456

5557
```java
56-
58+
class Solution {
59+
private int totalArea;
60+
private int[][] rects;
61+
private int[] a;
62+
private Random rand;
63+
64+
public Solution(int[][] rects) {
65+
this.totalArea = 0;
66+
this.rects = rects;
67+
this.a = new int[rects.length];
68+
this.rand = new Random();
69+
for (int i = 0; i < rects.length; i++) {
70+
totalArea += getArea(rects[i]);
71+
a[i] = totalArea;
72+
}
73+
}
74+
75+
private int getArea(int[] rect) {
76+
return (rect[2] - rect[0]) * (rect[3] - rect[1]);
77+
}
78+
79+
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};
84+
}
85+
86+
private int binarySearch(int[] nums, int target) {
87+
int lo = 0, hi = nums.length - 1;
88+
while (lo <= hi) {
89+
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;
93+
}
94+
return lo;
95+
}
96+
}
97+
98+
/**
99+
* Your Solution object will be instantiated and called as such:
100+
* Solution obj = new Solution(rects);
101+
* int[] param_1 = obj.pick();
102+
*/
57103
```
58104

59105
### Additional {#additional}

0 commit comments

Comments
 (0)