Given a list of positive integers w where w[i] is the weight of ith index, implement pickIndex() to randomly pick an index such that probability of picking index i is proportional to w[i].
Build a prefix sum array. Use random to get a number in [1, total]. Binary search for the first prefix sum >= random number.
- prefixSum[i] = sum of w[0..i].
- On pickIndex: r = random(1, totalSum).
- Binary search for first index where prefixSum[i] >= r.
- Return that index.
- Time Complexity: pickIndex: O(log N)
- Space Complexity: O(N)