Random Point in Non-overlapping Rectangles - Problem

You are given an array of non-overlapping axis-aligned rectangles rects where rects[i] = [ai, bi, xi, yi] indicates that (ai, bi) is the bottom-left corner point of the ith rectangle and (xi, yi) is the top-right corner point of the ith rectangle.

Design an algorithm to pick a random integer point inside the space covered by one of the given rectangles. A point on the perimeter of a rectangle is included in the space covered by the rectangle.

Any integer point inside the space covered by one of the given rectangles should be equally likely to be returned.

Note: An integer point is a point that has integer coordinates.

Implement the Solution class:

  • Solution(int[][] rects) Initializes the object with the given rectangles rects.
  • int[] pick() Returns a random integer point [u, v] inside the space covered by one of the given rectangles.

Input & Output

Example 1 — Single Rectangle
$ Input: rects = [[-2,-2,1,1]]
Output: [0,-1] (one possible answer)
💡 Note: Single rectangle from (-2,-2) to (1,1). Any point within this rectangle is valid, such as (0,-1).
Example 2 — Multiple Rectangles
$ Input: rects = [[-2,-2,-1,-1], [1,0,3,2]]
Output: [2,1] (one possible answer)
💡 Note: First rectangle has area 1, second has area 6. Point selection should be weighted by area, so second rectangle is 6 times more likely to be chosen.
Example 3 — Equal Area Rectangles
$ Input: rects = [[0,0,1,1], [2,2,3,3]]
Output: [3,3] (one possible answer)
💡 Note: Both rectangles have equal area (4 points each), so each rectangle should be selected with equal probability.

Constraints

  • 1 ≤ rects.length ≤ 100
  • rects[i].length == 4
  • -109 ≤ ai < xi ≤ 109
  • -109 ≤ bi < yi ≤ 109
  • xi - ai ≤ 2000
  • yi - bi ≤ 2000

Visualization

Tap to expand
Random Point in Non-overlapping Rectangles INPUT (-2,-2) (1,1) 16 integer points total rects = [[-2,-2,1,1]] [a, b, x, y] format Bottom-left: (a,b) Top-right: (x,y) Width=4, Height=4 Points=(4)(4)=16 ALGORITHM STEPS 1 Calculate Points Count points per rectangle (x-a+1)*(y-b+1) 2 Build Prefix Sum Cumulative point counts prefixSum = [16] 3 Weighted Random Pick rect by area weight Binary search prefix 4 Pick Point Random x,y in chosen rect Point Selection Formula: u = a + rand(0, x-a) v = b + rand(0, y-b) Each point equally likely FINAL RESULT (0,-1) Selected! Output: [0, -1] OK - Valid Point Point is within bounds -2 <= 0 <= 1 -2 <= -1 <= 1 Key Insight: Use weighted random selection based on rectangle areas (number of integer points). Build prefix sum of point counts, use binary search to select rectangle in O(log n), then uniformly sample a point within that rectangle. This ensures equal probability for all points. TutorialsPoint - Random Point in Non-overlapping Rectangles | Optimal Solution
Asked in
Google 15 Facebook 8 Amazon 6
28.5K Views
Medium Frequency
~25 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen