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(vector<int>& w) {
        for (auto x:w){
            total += x;
            prefixSum.push_back(total);
        }
    }

    int pickIndex() {
        // random generate from [1,total]
        int r=rand()%total+1;

        // find the first >= element
        auto it=lower_bound(prefixSum.begin(),prefixSum.end(),r);
        return it-prefixSum.begin();
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(w);
 * int param_1 = obj->pickIndex();
 */

497. Random Point in Non-overlapping Rectangles

矩形的内包括点的数量实际就是weight,根据weight先随机选择一个矩形,然后随机生成该矩形内的x和y即可。

class Solution {
public:
    vector<vector<int>> rects;
    vector<int> prefixSum;
    int total=0;

    Solution(vector<vector<int>>& rects) {
        this->rects = rects;
        for (auto rect:rects){
            int cur=(rect[2]-rect[0]+1)*(rect[3]-rect[1]+1);
            total += cur;
            prefixSum.push_back(total);
        }
    }

    vector<int> pick() {
        // select from [1,total]
        int r=rand()%total+1;
        auto it=lower_bound(prefixSum.begin(),prefixSum.end(),r);
        auto rect=rects[it-prefixSum.begin()];

        // randomly pick x from [rect[0],rect[2]], y from [rect[1],rect[3]]
        int x = rect[0] + rand()%(rect[2]-rect[0]+1);
        int y = rect[1] + rand()%(rect[3]-rect[1]+1);

        return {x,y};
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(rects);
 * vector<int> param_1 = obj->pick();
 */

Followup: 如果有overlap的矩形怎么办?

思路:把重复的长方形分成不重复的小块,然后用prefix sum进行二分查找

把一堆重叠长方形变成不重叠的长方形的具体思路可以看这个帖子:

递归解法 https://leetcode.com/problems/rectangle-area-ii/discuss/138028/Clean-Recursive-Solution-Java

迭代解法 https://leetcode.com/problems/rectangle-area-ii/discuss/137995/O(N2)-Maintain-a-List-of-Rectangle-Split-the-Rectangle-in-List-without-overlap

原文地址:https://www.cnblogs.com/hankunyan/p/11484238.html

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

LeetCode 528. Random Pick with Weight / 497. Random Point in Non-overlapping Rectangles的相关文章

[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

[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

select random item with weight 根据权重随机选出

http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/ 类似俄罗斯轮盘赌 select random item with weight 根据权重随机选出,布布扣,bubuko.com

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] 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 <=

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

[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

[Leetcode][JAVA] Clone Graph, Copy List with Random Pointer

Clone Graph: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label a