当前位置:网站首页>按权重随机选择[前缀和+二分+随机target]
按权重随机选择[前缀和+二分+随机target]
2022-06-25 12:07:00 【REN_林森】
前缀和+二分+随机target
前言
有序的地方就有二分法,尤其是数组元素全大于0的前缀和数组,二分就算利器。
一、按权重随机选择

二、前缀和+二分+随机target
package everyday.medium;
import java.util.Arrays;
import java.util.Random;
// 按权重随机选择
public class PickIndex {
/* 给一个权重数组w,随机按权重选则。 直接前缀和 + 随机整数 + 二分查找 == 返回idx,得到下标即可。 */
public PickIndex(int[] w) {
prefix = new int[w.length];
// 给前缀和赋值。
int sum = 0, idx = 0;
for (int i : w) prefix[idx++] = (sum += i);
System.out.println(Arrays.toString(prefix));
}
int[] prefix;
Random rand = new Random(1);
public int pickIndex() {
// 随机一个整数
// bug1:如果每次随机feed都一样,就产生相同的数。
// Random rand = new Random(System.currentTimeMillis());
// rand.nextInt(n)范围[0,n)
int target = rand.nextInt(prefix[prefix.length - 1]);
// 二分查找在前缀和中的位置。
return binarySearch(target + 1);
}
// 找比target 刚大或等于的地方。
private int binarySearch(int target) {
int low = 0, high = prefix.length - 1;
while (low < high) {
int mid = low + (high - low >>> 1);
int midVal = prefix[mid];
if (midVal < target) low = mid + 1;
else if (midVal > target) high = mid;//不能减一,因为往右走了,就回不来了。配合取下整来逼近。
else return mid;
}
return high;
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
Random rand = new Random(1);
System.out.println(rand.nextInt(100));
}
}
}
总结
1)有序数组/正数前缀和 与 二分法的紧密联系。
参考文献
[1] LeetCode 按权重随机选择
边栏推荐
- Polling and long polling
- Happy shopkeeper source code -- Introduction to happy shopkeeper system development mode
- 2022 Baidu collection batch automatic push assistant
- When MySQL queries fields in JSON format, it takes a property value of JSON data
- ECSHOP product attribute color specification size stock item No. automatic combination
- Flutter common commands and problems
- 15. Notes on the button style of WPF
- Go novice exploration road 2
- The first techo day Tencent technology open day in 2022 will be held online on June 28
- How to use ARIMA model for prediction?
猜你喜欢

What is Flink? What can Flink do?

ECSHOP commodity wholesale multi attribute multi specification multi inventory batch purchase ECSHOP wholesale plug-in ECSHOP multi attribute order

Upgrade opsenssh to 8.8p1

Go novice exploration road 1

A commonly used statistical modeling method -- difference analysis

2022 meisai topic C idea sharing + translation

How to use SPSS to do grey correlation analysis? Quick grasp of hand-to-hand Teaching

Flutter common commands and problems

Service charge and time setting code sharing involved in crmeb withdrawal process

Zhangxiaobai's road of penetration (VI) -- the idea and process of SQL injection and the concat series functions and information of SQL_ Schema database explanation
随机推荐
ARM V7 协处理器
Ubuntu uninstalling PHP
Lighten the source code -- lighten the app system development function introduction to the beautiful world lighten the app system development source code in China
The dist function of R language calculates the distance between two samples in dataframe data, returns the distance matrix between samples, and specifies the distance calculation method through the me
Swagger document generated by node project API in vscode
ECSHOP commodity wholesale multi attribute multi specification multi inventory batch purchase ECSHOP wholesale plug-in ECSHOP multi attribute order
Go novice exploration pause
High imitation blue playing network disk file sharing to make money network disk PHP system source code
Go defer little knowledge
The network traceroute command is used to determine the path through which IP packets access the destination address.
Implementing Domain Driven Design - using the ABP framework - Summary of a series of articles
R language dplyr package summary_ The at function calculates the count number, mean and median of multiple data columns (specified by vectors) in the dataframe data, and specifies na RM parameter, spe
Why should Apple change objc_ Type declaration for msgsend
How to use SPSS to do grey correlation analysis? Quick grasp of hand-to-hand Teaching
2022 meisai e topic ideas sharing + translation
Wechat forbids sharing
Why do we do autocorrelation analysis? Explain application scenarios and specific operations
[oceanbase] Introduction to oceanbase and its comparison with MySQL
Explain AHP in human language (very detailed principle + simple tool implementation)
PHP appends the same elements to a two-dimensional array