528. 按权重随机选择

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:

def __init__(self, w: List[int]):
self.pre = list(accumulate(w))
self.sum_ = sum(w)
print(self.sum_)
def pickIndex(self) -> int:
x = random.randint(1,self.sum_)
return bisect.bisect_left(self.pre,x)




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