g0301_0400.s0398_random_pick_index.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0301_0400.s0398_random_pick_index;
// #Medium #Hash_Table #Math #Randomized #Reservoir_Sampling
// #2022_07_15_Time_102_ms_(83.94%)_Space_81.3_MB_(76.10%)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
@SuppressWarnings("java:S2245")
public class Solution {
// O(n) time | O(n) space
private Map> map;
private Random rand;
public Solution(int[] nums) {
map = new HashMap<>();
rand = new Random();
for (int i = 0; i < nums.length; i++) {
map.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
}
}
public int pick(int target) {
List list = map.get(target);
return list.get(rand.nextInt(list.size()));
}
}
/*
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/