Random Point in Non-overlapping Rectangles

Medium

Description

Given non-overlapping rectangles, randomly pick an integer point inside with probability proportional to area.

Examples

Input:rects = [[-2,-2,1,1],[2,2,4,6]]
Output:Point in one of the rectangles
Explanation:

Area-weighted selection.

Input:rects = [[1]]
Output:Point in one of the rectangles
Explanation:

Minimal case with a single-element matrix.

Input:rects = [[0,0,2,1],[3,1,5,4],[0,2,1,5]]
Output:Point in one of the rectangles
Explanation:

Three rectangles with areas 2, 6, and 3 respectively. The middle rectangle [3,1,5,4] has the largest area (6) so points should be selected from it most frequently, while the first rectangle [0,0,2,1] with area 2 should be selected least frequently.

Constraints

  • 1 ≤ rects.length ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!