g0501_0600.s0528_random_pick_with_weight.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0528_random_pick_with_weight;
// #Medium #Math #Binary_Search #Prefix_Sum #Randomized
import java.security.SecureRandom;
import java.util.Comparator;
import java.util.Objects;
import java.util.TreeSet;
public class Solution {
private int prefix;
private final SecureRandom random;
private final TreeSet treeSet;
public Solution(int[] w) {
prefix = 0;
treeSet = new TreeSet<>(Comparator.comparingInt(a -> a[0]));
for (int i = 0; i < w.length; i++) {
prefix += w[i];
treeSet.add(new int[] {prefix, i});
}
random = new SecureRandom();
}
public int pickIndex() {
int target = random.nextInt(prefix) + 1;
return Objects.requireNonNull(treeSet.ceiling(new int[] {target, 1}))[1];
}
}