【leetcode 蓄水池抽样 C++】497. Random Point in Non-overlapping Rectangles

497. Random Point in Non-overlapping Rectangles

在这里插入图片描述

class Solution {
    
    
public:
    vector<vector<int>> rects;
    Solution(vector<vector<int>>& rects) {
    
    
        this->rects = rects;
    }
    
    vector<int> pick() {
    
    
        int num = 0;
        vector<int> select;
        for(auto rect : rects) {
    
    
            int temp = (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1);
            num += temp;
            if(rand() % num < temp) select = rect;
        }
        return {
    
    rand() % (select[2] - select[0] + 1) + select[0], rand() % (select[3] - select[1] + 1) + select[1]};
    }
};

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

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/114191215