Imagine you're designing a random location generator for a video game where players can spawn at any integer coordinate within designated safe zones (rectangles). You need to ensure that every possible spawn point has an equal probability of being selected!
You're given an array of non-overlapping axis-aligned rectangles where each rectangle is defined as [a_i, b_i, x_i, y_i]:
(a_i, b_i)= bottom-left corner(x_i, y_i)= top-right corner
Your task: Design an algorithm that can pick a random integer point inside the space covered by these rectangles. Points on the perimeter are included, and every integer point should be equally likely to be returned.
Example: If you have rectangles [[1,1,3,3], [2,2,4,4]], your algorithm should be able to return any integer coordinate like [1,1], [3,2], [4,4], etc., with proper probability distribution.
Input & Output
Visualization
Time & Space Complexity
Each pick operation takes constant time
Only stores the input rectangles array
Constraints
- 1 โค rects.length โค 100
- rects[i].length == 4
- -109 โค ai < xi โค 109
- -109 โค bi < yi โค 109
- xi - ai โค 2000
- yi - bi โค 2000
- All rectangles are non-overlapping
- At most 104 calls will be made to pick