Random Point in Non-overlapping Rectangles

MediumArray

Description

Given non-overlapping rectangles, randomly pick an integer point inside with probability proportional to area. Return any valid point as an array [x, y].

Examples

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

Any integer point inside either rectangle is a valid output. Points from larger rectangles should appear more often over many calls.

Input:rects = [[1,1,1,1]]
Output:[1,1]
Explanation:

A rectangle with corners at (1,1) and (1,1) is a single point. The only valid integer point that can be selected is [1,1].

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

The selected point comes from one of the rectangles, so the output is "Point in one of the rectangles".

Constraints

  • 1 ≤ rects.length ≤ 100

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.