|
4 | 4 |
|
5 | 5 | 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. |
6 | 6 |
|
7 | | -**Note: |
| 7 | +**Note: |
8 | 8 | ** |
9 | 9 |
|
10 | 10 | 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 |
43 | 43 |
|
44 | 44 | 1. Binary Search |
45 | 45 | 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\) |
51 | 53 | 2. asd |
52 | 54 |
|
53 | 55 | ### Solution |
54 | 56 |
|
55 | 57 | ```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 | + */ |
57 | 103 | ``` |
58 | 104 |
|
59 | 105 | ### Additional {#additional} |
|
0 commit comments