You are given a 0-indexed array of positive integers w where w[i] describes the weight of the ith index.
You need to implement the function pickIndex(), which randomly picks an index in the range [0, w.length - 1] (inclusive) and returns it. The probability of picking an index i is w[i] / sum(w).
For example, if w = [1, 3], the probability of picking index 0 is 1 / (1 + 3) = 0.25 (i.e., 25%), and the probability of picking index 1 is 3 / (1 + 3) = 0.75 (i.e., 75%).
Example 1:
Input
["Solution","pickIndex"]
[[[1]],[]]
Output
[null,0]
Explanation
Solution solution = new Solution([1]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
Example 2:
Input
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
Output
[null,1,1,1,1,0]
Explanation
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
[null,1,1,1,1,0]
[null,1,1,1,1,1]
[null,1,1,1,0,0]
[null,1,1,1,0,1]
[null,1,0,1,0,0]
......
and so on.
int n = preSum.length;
// target 取值范围是闭区间 [1, preSum[n - 1]]
int target = rand.nextInt(preSum[n - 1]) + 1;
// 搜索左侧边界的二分搜索
int left_bound(int[] nums, int target) {
if (nums.length == 0) return -1;
int left = 0, right = nums.length;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
right = mid;
} else if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid;
}
}
return left;
}
class Solution {
// 前缀和数组
private int[] preSum;
private Random rand = new Random();
public Solution(int[] w) {
int n = w.length;
// 构建前缀和数组,偏移一位留给 preSum[0]
preSum = new int[n + 1];
preSum[0] = 0;
// preSum[i] = sum(w[0..i-1])
for (int i = 1; i <= n; i++) {
preSum[i] = preSum[i - 1] + w[i - 1];
}
}
public int pickIndex() {
int n = preSum.length;
// 在闭区间 [1, preSum[n - 1]] 中随机选择一个数字
int target = rand.nextInt(preSum[n - 1]) + 1;
// 获取 target 在前缀和数组 preSum 中的索引
// 搜索左侧边界的二分搜索
int left = 0, right = n;
while (left < right) {
int mid = left + (right - left) / 2;
if (preSum[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
// preSum 的索引偏移了一位,还原为权重数组 w 的索引
return left - 1;
}
}
JAVA Version:
class Solution {
private Random random;
private int[] preSum;
public Solution(int[] w) {
random = new Random();
preSum = new int[w.length+1];
preSum[0] = 0;
for(int i = 1;i< w.length+1; i++)
{
preSum[i] = preSum[i-1] + w[i-1];
}
}
public int pickIndex() {
int len = preSum.length;
int target = random.nextInt(preSum[len-1]) + 1;
int left = 1;
int right = preSum.length-1;
while(left + 1 < right)
{
int mid = left+(right-left)/2;
if(preSum[mid] == target)
{
right = mid;
}
else if(preSum[mid] > target)
{
right = mid;
}
else if(preSum[mid] < target)
{
left = mid;
}
}
if(preSum[left] >= target) return left-1;
if(preSum[right] >= target) return right-1;
return left-1;
}
}