ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

519 随机翻转矩阵

2021-11-27 20:34:46  阅读:128  来源: 互联网

标签:idx int bucket 矩阵 Solution 519 book bucketSize 翻转


思路:

这道题起始很简单就是说每次抽取一个数,然后把这个数消去再下一次随机抽取。

这里思路有两种:一是如果抽到这个数,就不需要把它从数组中删除,而是将他的值和最后一个元素的值进行互换,然后把SIZE - 1,这样就不需要用set,list这种,可以直接用下标访问元素,很快;或者如果说这个数的值是发生改变了的,利用一个数组存储发生改变后的值,然后再返回答案;二是利用分块的思想,我们不是要求第几个大于0的数嘛,这就是很明显的分块思想,每一块都是根号m * n个数,用cnt[i]表示第i块中0的数量,这样只要找到第一个sum[i - 1] < x < sum[i]的就可以了

代码:

思路一:

class Solution {
public:
    // vector<int> cur;
    unordered_map<int, int> book;
    // vector<int> src;
    int sz;
    int _n;
    int src = 0;
    Solution(int m, int n) {
        _n = n;
        // end = m * n - 1;
        // cur.resize(m * n);
        // for(int i = 0;i<=end;i++){
        //     // src.push_back(i);
        //     cur[i] = i;
        // }
        sz = src = m * n;
    }
    
    vector<int> flip() {
        int idx = next();
        vector<int> ans;
        if(book.count(idx)){
            ans = {book[idx] / _n, book[idx] % _n};
        }else{
            ans = {idx / _n, idx % _n};
        }

        if(book.count(sz - 1)){
            book[idx] = book[sz - 1];
        }else{
            book[idx] = sz - 1;
        }
        sz--;

        return ans;
    }
    
    void reset() {
        // cur = src;
        sz = src;
        book.clear();
    }
    inline int next(){
        return rand() % sz;
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(m, n);
 * vector<int> param_1 = obj->flip();
 * obj->reset();
 */

思路二:

class Solution {
public:
    Solution(int m, int n) {
        this->m = m;
        this->n = n;
        total = m * n;
        bucketSize = sqrt(m * n);
        for (int i = 0; i < total; i += bucketSize) {
            buckets.push_back({});
        }
        srand(time(nullptr));
    }

    vector<int> flip() {
        int x = rand() % total;
        int sumZero = 0;
        int curr = 0;
        total--;

        for (auto & bucket : buckets) {
            if (sumZero + bucketSize - bucket.size() > x) {
                for (int i = 0; i < bucketSize; ++i) {
                    if (!bucket.count(curr + i)) {
                        if (sumZero == x) {
                            bucket.emplace(curr + i);
                            return {(curr + i) / n, (curr + i) % n};
                        } 
                        sumZero++;
                    }
                }
            }
            curr += bucketSize;
            sumZero += bucketSize - bucket.size();
        }
        
        return {};
    }

    void reset() {
        for (auto & bucket : buckets) {
            bucket.clear();
        }
        total = m * n;
    }
private:
    int m;
    int n;
    int bucketSize;
    int total;
    vector<unordered_set<int>> buckets;
};

 

标签:idx,int,bucket,矩阵,Solution,519,book,bucketSize,翻转
来源: https://www.cnblogs.com/TherLF/p/15612959.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有