497. Random Point in Non overlapping Rectangles (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution:
    def __init__(self, rects: List[List[int]]):
        self.weights = []
        s = 0
        for x1, y1, x2, y2 in rects:
            s += (x2 - x1 + 1) * (y2 - y1 + 1)
            self.weights.append(s)
        self.rects = rects

    def pick(self) -> List[int]:
        w = random.randint(1, self.weights[-1])
        i = bisect.bisect_left(self.weights, w)
        x = random.randint(self.rects[i][0], self.rects[i][2])
        y = random.randint(self.rects[i][1], self.rects[i][3])
        return [x, y]
    
# Your Solution object will be instantiated and called as such:
# obj = Solution(rects)
# param_1 = obj.pick()
# param_1 = obj.pick()