528. Random Pick with Weight (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution(object):

    def __init__(self, w):
        """
        :type w: List[int]
        """
        for i in range(len(w) - 1):
            w[i + 1] += w[i]
        self.w = w

    def pickIndex(self):
        """
        :rtype: int
        """
        val = random.random() * self.w[-1]
        return bisect.bisect_left(self.w, val)

# Your Solution object will be instantiated and called as such:
# obj = Solution(w)
# param_1 = obj.pickIndex()