← 返回 meta 的题目列表Weighted Random Index Picker Implementation
类型:online_judge
meta
Given an array of positive integers w where w[i] describes the weight of i-th object, implement a function pickIndex which randomly picks an index i from w with that probability proportional to its weight.
Implement function:
Solution(int[] w): Initialize the object and store the array in it.
int pickIndex(): Return a random index, the probability of choosing index i is proportional to w[i].
Maximum input size: The length of w does not exceed 10^4 and the weight does not exceed 10^5.
Example
Input
[1, 3]