[LC] 398. Random Pick Index

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

Note:
The array size can be very large. Solution that uses too much extra space will not pass the judge.

Example:

int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums);

// pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(3);

// pick(1) should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(1);
class Solution {

    private int[] arr;
    private Random rand;

    public Solution(int[] nums) {
        this.arr = nums;
        this.rand = new Random();
    }

    public int pick(int target) {
        int count = 0;
        int res = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != target) {
                continue;
            }
            count += 1;
            int randNum = rand.nextInt(count);
            if (randNum == 0) {
                res = i;
            }
        }
        return res;
    }
}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * int param_1 = obj.pick(target);
 */

原文地址:https://www.cnblogs.com/xuanlu/p/12181760.html

时间: 2024-08-17 05:20:26

[LC] 398. Random Pick Index的相关文章

398 Random Pick Index 随机数索引

给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] nums = new int[] {1,2,3,3,3};Solution solution = new Solution(nums);// pick(3) 应该返回索引 2,3 或者 4.每个索引的返回概率应该相等.solution.pick(3);// pick(1) 应该返回 0.因为只有nums[

398. Random Pick Index

https://leetcode.com/problems/random-pick-index/description/ Reservoir Sampling class Solution { public: vector<int> m; Solution(vector<int> nums) { m = nums; } int pick(int target) { int res = -1; for (int i = 0, cnt = 0; i < m.size(); i++

Random Pick Index

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Note: The array size can be very large. Solution that uses too much extra sp

leetcode_398 Random Pick Index(Reservoir Sampling)

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Note:The array size can be very large. Solution that uses too much extra spa

Leetcode: Random Pick Index

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Note: The array size can be very large. Solution that uses too much extra sp

LeetCode 528. Random Pick with Weight / 497. Random Point in Non-overlapping Rectangles

528. Random Pick with Weight 根据weight随机选取一个数,用 Prefix Sum+Binary Search 来解决. https://www.geeksforgeeks.org/random-number-generator-in-arbitrary-probability-distribution-fashion/ class Solution { public: vector<int> prefixSum; int total=0; Solution(v

[LeetCode] Random Pick with Blacklist 带黑名单的随机选取

Given a blacklist B containing unique integers from [0, N), write a function to return a uniform random integer from [0, N) which is NOT in B. Optimize it such that it minimizes the call to system’s Math.random(). Note: 1 <= N <= 1000000000 0 <=

[LeetCode] Random Pick with Weight 根据权重随机取点

Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight. Note: 1 <= w.length <= 10000 1 <= w[i] <= 10^5 pickIndex will be called at

[leetcode]528. Random Pick with Weight按权重挑选索引

Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight. Note: 1 <= w.length <= 10000 1 <= w[i] <= 10^5 pickIndex will be called at