算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
今天和大家聊的问题叫做 按权重随机选择,我们先来看题面:
https://leetcode-cn.com/problems/random-pick-with-weight/
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%).
给你一个 下标从 0 开始 的正整数数组 w ,其中 w[i] 代表第 i 个下标的权重。
请你实现一个函数 pickIndex ,它可以 随机地 从范围 [0, w.length - 1] 内(含 0 和 w.length - 1)选出并返回一个下标。选取下标 i 的 概率 为 w[i] / sum(w) 。
例如,对于 w = [1, 3],挑选下标 0 的概率为 1 / (1 + 3) = 0.25 (即,25%),而选取下标 1 的概率为 3 / (1 + 3) = 0.75(即,75%)。
示例
示例 1:
输入:
["Solution","pickIndex"]
[[[1]],[]]
输出:
[null,0]
解释:
Solution solution = new Solution([1]);
solution.pickIndex(); // 返回 0,因为数组中只有一个元素,所以唯一的选择是返回下标 0。
示例 2:
输入:
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
输出:
[null,1,1,1,1,0]
解释:
Solution solution = new Solution([1, 3]);
solution.pickIndex(); // 返回 1,返回下标 1,返回该下标概率为 3/4 。
solution.pickIndex(); // 返回 1
solution.pickIndex(); // 返回 1
solution.pickIndex(); // 返回 1
solution.pickIndex(); // 返回 0,返回下标 0,返回该下标概率为 1/4 。
由于这是一个随机问题,允许多个答案,因此下列输出都可以被认为是正确的:
[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]
......
诸若此类。
解题
https://www.cnblogs.com/linrj/p/13972905.html
要按照概率随机选择一个数,可以将数组的值看作一个区间上的长度,比如题目给的例子,当w = [1, 3]时,我们可以假设有一个一维的区间,区间前1/4是第0个数,区间的后3/4是第1个数。
区间总长度4也就是w数组所有数的和。
我们可以在总长度范围(0~4)内随机选择一个数,假设这个数是0~1,那么就返回0,如果这个数是1~4,那么就返回1。
这样就解决了按照概率随机返回的问题。
但是怎么判断我们随机选择的数该返回什么值呢?
这里需要用到前缀和,比如上面的例子w = [1, 3],我们可以得到前缀和preSum = [0, 1, 4](计算前缀和时,一般前面要预留一个0,也就是w[0] + ... + w[i]的前缀和实际上是preSum[i + 1],这是有前缀和的计算公式决定的)。
我们在总长度范围内随机取的数在区间内处于哪一个前缀和的范围内,就返回那个前缀和对应的下标,比如我们取到随机数oneRandNum = 2,那么在前缀和区间里第一个大于等于它的前缀和是下标为2(在原数组中下标为1)的前缀和4,这时我们需要返回前缀和为4的那个下标2(在原数组中下标为1),所以我们需要返回lower_bound(preSum.begin() + 1, preSum.end(), oneRandNum) - preSum.begin() - 1
class Solution {
public:
vector<int> preSum;
int n;
Solution(vector<int>& w) {
n = w.size();
preSum.resize(n + 1, 0);
for(int i = 1; i <= n; ++i) {
preSum[i] = preSum[i - 1] + w[i - 1];
}
}
int pickIndex() {
int oneRandNum = rand() % preSum[n] + 1; // 取一个随机数,+1是为了将随机数映射到范围[1, 2, ... preSum[n]]内
return lower_bound(preSum.begin() + 1, preSum.end(), oneRandNum) - preSum.begin() - 1; // 找到第一个大于等于oneRandNum的前缀和在原数组中对应的下标,就是答案
}
};
好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。
上期推文: