forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
46 lines (34 loc) · 798 Bytes
/
main.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
/// Source : https://leetcode.com/problems/random-flip-matrix/description/
/// Author : liuyubobobo
/// Time : 2018-08-28
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
/// Rejection Sampling
///
/// Time Complexty: O(m*n)
/// Space Complexity: O(min(len(call of flip), m*n))
class Solution {
private:
int m, n;
unordered_set<int> ones;
public:
Solution(int n_rows, int n_cols): m(n_rows), n(n_cols) {
ones.clear();
}
vector<int> flip() {
int randNum;
do{
randNum = rand() % (m * n);
}while(ones.find(randNum) != ones.end());
ones.insert(randNum);
return {randNum / n, randNum % n};
}
void reset() {
ones.clear();
}
};
int main() {
return 0;
}