forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random-flip-matrix.cpp
47 lines (39 loc) · 1.03 KB
/
random-flip-matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Time: ctor: O(1)
// flip: O(1)
// reset: O(min(f, r * c))
// Space: O(min(f, r * c))
class Solution {
public:
Solution(int n_rows, int n_cols) :
n_rows_(n_rows),
n_cols_(n_cols),
n_(n_rows * n_cols),
gen_{(random_device())()} {
}
vector<int> flip() {
uniform_int_distribution<int> uni(0, --n_);
const auto target = uni(gen_);
int x = get(target, target);
lookup_[target] = get(n_, n_);
return {x / n_cols_, x % n_cols_};
}
void reset() {
lookup_.clear();
n_ = n_rows_ * n_cols_;
}
private:
int get(int key, int default_value) {
return lookup_.count(key) ? lookup_[key] : default_value;
}
int n_rows_;
int n_cols_;
int n_;
unordered_map<int, int> lookup_;
default_random_engine gen_;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(n_rows, n_cols);
* vector<int> param_1 = obj.flip();
* obj.reset();
*/