Leetcode 497. Random Point in Non
Given a list of non-overlapping axis-aligned rectangles rects
, write a function that randomly and uniformly picks an integer point in the space covered by the rectangles.
Each rectangle rects[i]
is represented as a list, [x1, y1, x2, y2]
, where (x1, y1)
is the coordinate of its bottom-left corner and (x2, y2)
is the coordinate of its top-right corner.
You need to implement the class Solution
and the function pick()
such that each point in the rectangles is chosen with uniform randomness.
class Solution {
public Solution(int[][] rects) {
}
public int[] pick() {
}
}
pick()
function called relative to initialization?
(x2 - x1 + 1) * (y2 - y1 + 1)
.import java.util.Random;
class Solution {
private int[][] rects;
private int[] areas;
private int totalArea;
private Random rand;
public Solution(int[][] rects) {
this.rects = rects;
this.areas = new int[rects.length];
this.totalArea = 0;
this.rand = new Random();
// Pre-compute the area (number of points) for each rectangle
for (int i = 0; i < rects.length; i++) {
int[] rect = rects[i];
int area = (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1);
totalArea += area;
areas[i] = totalArea; // This keeps a cumulative sum of areas
}
}
public int[] pick() {
int target = rand.nextInt(totalArea); // Pick a random area position
int low = 0, high = areas.length - 1;
// Binary search to find the rectangle corresponding to the target
while (low < high) {
int mid = low + (high - low) / 2;
if (areas[mid] > target) {
high = mid;
} else {
low = mid + 1;
}
}
// Low now points to the rectangle we need
int[] rect = rects[low];
// Calculate random coordinates inside the found rectangle
int x = rect[0] + rand.nextInt(rect[2] - rect[0] + 1);
int y = rect[1] + rand.nextInt(rect[3] - rect[1] + 1);
return new int[]{x, y};
}
}
Solution
constructor):
Overall, the pick
method has a time complexity of (O(\log n)).
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?