Skip to content
LC-0528 Medium LeetCode

528. Random Pick with Weight

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 48% Topics: Array, Math, Binary Search, Prefix Sum, Randomized
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  ctor: O(n)
#        pickIndex: O(logn)
# Space: O(n)

import random
import bisect


class Solution(object):

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

    def pickIndex(self):
        """
        :rtype: int
        """
        target = random.randint(0, self.__prefix_sum[-1]-1)
        return bisect.bisect_right(self.__prefix_sum, target)



Solution from kamyu104/LeetCode-Solutions · MIT