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++) {
            if (m[i] == target) {
                cnt++;
                if (rand() % cnt == 0)
                    res = i;
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/JTechRoad/p/8998254.html

时间: 2024-08-01 15:58:46

398. Random Pick Index的相关文章

[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 spa

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[

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