TomoLink
CompaniesZeptoData Structures & AlgorithmsRandom Point in Non-overlapping Rectangles
DSA
MediumArray

Random Point in Non-overlapping Rectangles

arraymathbinary search

Problem Statement

You are given an array of non-overlapping axis-aligned rectangles rects where rects[i] = [a _i , b _i , x _i , y _i ] indicates that (a _i , b _i ) is the bottom-left corner point of the i ^th rectangle and (x _i , y _i ) is the top-right corner point of the i ^th 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 that 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.

Examples

Example 1
Input:
Output:

Constraints

1 <= rects.length <= 100
rects[i].length == 4
-10 ^9 <= a _i < x _i <= 10 ^9
-10 ^9 <= b _i < y _i <= 10 ^9
x _i - a _i <= 2000
y _i - b _i <= 2000
All the rectangles do not overlap.
At most 10 ^4 calls will be made to pick .
🤔
Medium
Difficulty
Topic Info
ModuleDSA
CategoryArray
Sub-topicMath
Tags
arraymathbinary searchreservoir samplingprefix sumordered setrandomized
Navigation
Random Point in Non-overlapping Rectangles [Medium] | Zepto Dsa | TomoLink