Problem Description
Given an integer array w where w[i] represents the weight of index i, implement a function pickIndex() that returns an index with a probability proportional to its weight. That is, the probability of picking index i is w[i] / sum(w).
Key Insights
- Build a prefix sum array from the weights.
- Calculate the total sum of the weights.
- Generate a random number in the range [1, total sum] and use binary search to find the corresponding index in the prefix sum array.
- Using binary search ensures efficient selection in O(log n) time for each call of pickIndex().
Space and Time Complexity
Time Complexity: O(n) for constructing the prefix sum array, and O(log n) per pickIndex() call. Space Complexity: O(n) for storing the prefix sum array.
Solution
The solution leverages a prefix sum array where each entry at index i represents the cumulative sum of weights up to i. After computing the total weight, a random integer is generated between 1 and the total sum (inclusive). A binary search is then used to find the first index where the prefix sum is greater than or equal to the random number. This approach ensures that the index is selected with a probability proportional to its weight.