Posted on

Description

Submission

class Solution {
    unordered_map<int, int> replace;
    int ptr;
    int m;
    int n;
public:
    Solution(int m, int n) 
        :
        m(m),
        n(n),
        ptr(m*n-1)
    {}
    
    vector<int> flip() {
        int r = rand() % (ptr + 1);

        int t = r;
        if(replace.find(r) != replace.end()) t = replace[r];
        int row = t / n;
        int col = t % n;

        if(replace.find(ptr) != replace.end()) replace[r] = replace[ptr--];
        else replace[r] = ptr--;

        return {row, col};
    }
    
    void reset() {
        replace.clear();
        ptr = m * n - 1;
    }
};

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

Leave a Reply

Your email address will not be published. Required fields are marked *