You’re given an array of positive integers, w, where w[i] is the weight of the index.
Implement the function pickIndex(), which performs weighted random selection to return an index from the w array. The larger the value of w[i], the heavier the weight is, and the higher the chances of its index being picked.
Suppose that the array consists of the weights . In this case, the probabilities of picking the indexes will be as follows:
Index 0:
Index 1:
Index 2:
Constraints:
w.length
w[i]
pickIndex() will be called at most times.
Note: Since we’re randomly choosing from the options, there is no guarantee that in any specific run of the program, any of the elements will be selected with the exact expected frequency.
You’re given an array of positive integers, w, where w[i] is the weight of the index.
Implement the function pickIndex(), which performs weighted random selection to return an index from the w array. The larger the value of w[i], the heavier the weight is, and the higher the chances of its index being picked.
Suppose that the array consists of the weights . In this case, the probabilities of picking the indexes will be as follows:
Index 0:
Index 1:
Index 2:
Constraints:
w.length
w[i]
pickIndex() will be called at most times.
Note: Since we’re randomly choosing from the options, there is no guarantee that in any specific run of the program, any of the elements will be selected with the exact expected frequency.