bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 497. Random Point in Non-overlapping Rectangles

497. Random Point in Non-overlapping Rectangles

Leetcode Solutions

Weighted Random Selection of Rectangles

  1. Calculate the area of each rectangle and store these values.
  2. Compute a prefix sum array of the areas to use as weights for random selection.
  3. To pick a random rectangle, generate a random integer between 1 and the sum of all rectangle areas.
  4. Use binary search to find the rectangle corresponding to the random integer based on the prefix sum array.
  5. Once the rectangle is chosen, generate a random point within the rectangle's boundaries.
  6. Return the random point.
UML Thumbnail

Sequential Rectangle Selection and Random Point Generation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...