Skip to content
Problem 497 · LeetCode 497(opens in a new tab)

Random Point in Non-overlapping Rectangles

MediumBinary search

You are given rects, a list of axis-aligned rectangles that never overlap. Each one is written [a, b, x, y]: (a, b) is its bottom-left corner and (x, y) its top-right corner. Points on a rectangle's border count as inside it.

Build a class Solution. The constructor receives rects. Each call to pick() returns one point [u, v] with whole-number coordinates that lies inside one of the rectangles. Across many calls, every such point must be equally likely to come back.

Example 1

Input:  Solution([[0, 0, 1, 1], [3, -1, 5, 0]]), then pick()
Output: any of the 10 points, each with chance 1/10, e.g. [3, 0]
Why:    4 points in the first rectangle, 6 in the second
2 more examples and the constraints

Example 2

Input:  Solution([[-3, 5, -2, 6]]), then pick(), pick()
Output: two of [-3, 5], [-2, 5], [-3, 6], [-2, 6]
Why:    a 2 × 2 block of points, a quarter each

Example 3

Input:  Solution([[0, 0, 1, 1], [10, 10, 12, 12]]), then pick()
Output: a point of the second rectangle 9 times in 13
Why:    it holds 9 of the 13 points

Constraints

  • 1 ≤ number of rectangles ≤ 100
  • −1,000,000,000 ≤ a < x ≤ 1,000,000,000 and −1,000,000,000 ≤ b < y ≤ 1,000,000,000
  • x − a ≤ 2,000 and y − b ≤ 2,000
  • Rectangles do not overlap
  • At most 10,000 calls to pick

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. List every pointO(P) setup
  2. Prefix sums + binary searchO(log n) pick

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative setup work for 100 large rectanglesVerdict
List every pointO(P) setup
Fair, but stores every point
Prefix sums + binary searchO(log n) pick
Best; tiny memory, fast picks

n = number of rectangles, P = total integer points (up to about 400 million).

Edge cases to test

  • Border points. A rectangle [0, 0, 1, 1] has 4 points, not 1: count (x − a + 1) × (y − b + 1).
  • One rectangle. The search always lands on index 0; nothing is subtracted.
  • Huge coordinates. Corners near ±1,000,000,000 are fine: sizes stay small and the total fits in 32 bits.
  • Very different sizes. A thin strip and a large square must be picked in proportion to their point counts.

Common mistakes

  • Picking a rectangle uniformly first, which favours points in small rectangles.
  • Weighting by area (x − a) × (y − b), which ignores the border points.
  • Searching for the first prefix entry at or above k instead of strictly above it, which is off by one.

How to talk through it

  1. 1Clarify. Do border points count? Uniform over points or over rectangles? How many picks?
  2. 2Give the simple version. List every point and choose one; say why 400 million points is too many.
  3. 3Number the points. Count points per rectangle into prefix sums; a draw k names one point.
  4. 4Code the pick. Binary search the prefix, subtract, then column = k % width, row = k / width.
  5. 5Test out loud. Check a 2 × 2 rectangle gives 4 points and that the search uses strictly greater.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?