Practice Random Point in Non-overlapping Rectangles
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Random Point in Non-overlapping Rectangles
Problem Statement
Solution(rects) receives nonoverlapping closed rectangles [a,b,x,y], from bottom-left (a,b) to top-right (x,y). pick() returns a uniformly selected integer-coordinate point across their union. Boundary lattice points count. Practice approach: Weight each rectangle by (x-a+1)*(y-b+1), not by geometric area. Prefix-sum those counts and select a uniform global lattice-point index, then map its offset to a coordinate in the chosen rectangle. Uniformly choosing rectangles would bias smaller rectangles. Use a sufficiently wide integer type for cumulative counts.
Source: Random Point in Non-overlapping Rectangles on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
["Solution","pick","pick"] [[[[0,0,1,1],[3,0,5,1]]],[],[]]
[null,[1,1],[4,0]]
One possible sequence is shown. The first rectangle contains four lattice points and the second six, so their selection probabilities are 4/10 and 6/10.
Constraints
Related LeetCode Problems
Practice more problems similar to Random Point in Non-overlapping Rectangles.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.