Last updated
Last updated
Given an array w
of positive integers, where w[i]
describes the weight of index i
(0-indexed), write a function pickIndex
which randomly picks an index in proportion to its weight.
For example, given an input list of values w = [2, 8], when we pick up a number out of it, the chance is that 8 times out of 10 we should pick the number 1 as the answer since it's the second element of the array (w[1] = 8).
Example 1:
Example 2:
Constraints:
1 <= w.length <= 10000
1 <= w[i] <= 10^5
pickIndex
will be called at most 10000
times.