- Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathRandomFlipMatrix519.java
More file actions
Latest commit
100 lines (86 loc) · 3.02 KB
/
Copy pathRandomFlipMatrix519.java
File metadata and controls
100 lines (86 loc) · 3.02 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* You are given the number of rows n_rows and number of columns n_cols of a
* 2D binary matrix where all values are initially 0. Write a function flip
* which chooses a 0 value uniformly at random, changes it to 1, and then
* returns the position [row.id, col.id] of that value. Also, write a function
* reset which sets all values back to 0. Try to minimize the number of calls
* to system's Math.random() and optimize the time and space complexity.
*
* Note:
* 1 <= n_rows, n_cols <= 10000
* 0 <= row.id < n_rows and 0 <= col.id < n_cols
* flip will not be called when the matrix has no 0 values left.
* the total number of calls to flip and reset will not exceed 1000.
*
* Example 1:
* Input:
* ["Solution","flip","flip","flip","flip"]
* [[2,3],[],[],[],[]]
* Output: [null,[0,1],[1,2],[1,0],[1,1]]
*
* Example 2:
* Input:
* ["Solution","flip","flip","reset","flip"]
* [[1,2],[],[],[],[]]
* Output: [null,[0,0],[0,1],null,[0,0]]
* Explanation of Input Syntax:
* The input is two lists: the subroutines called and their arguments.
* Solution's constructor has two arguments, n_rows and n_cols. flip and
* reset have no arguments. Arguments are always wrapped with a list, even
* if there aren't any.
*/
publicclassRandomFlipMatrix519 {
/**
* https://leetcode.com/problems/random-flip-matrix/discuss/154053/Java-AC-Solution-call-Least-times-of-Random.nextInt()-function
*/
classSolution {
privateMap<Integer, Integer> map = newHashMap<>();
privateintsize;
privateintN;
privateintnCols;
privateRandomrand = newRandom();
publicSolution(intn_rows, intn_cols) {
this.N = n_rows * n_cols;
this.nCols = n_cols;
this.size = this.N;
}
publicint[] flip() {
intidx = rand.nextInt(this.size--);
intval = map.getOrDefault(idx, idx);
map.put(idx, map.getOrDefault(this.size, this.size));
returnnewint[]{val / this.nCols, val % this.nCols};
}
publicvoidreset() {
map.clear();
this.size = this.N;
}
}
classSolution2 {
privateintN;
privateintnCols;
privateRandomrand = newRandom();
privateSet<Integer> set = newHashSet<>();
publicSolution(intn_rows, intn_cols) {
this.N = n_rows * n_cols;
this.nCols = n_cols;
// this.size = this.N;
}
publicint[] flip() {
intval = rand.nextInt(this.N);
while (set.contains(val)) {
val = rand.nextInt(this.N);
}
set.add(val);
returnnewint[]{val / this.nCols, val % this.nCols};
}
publicvoidreset() {
this.set.clear();
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(n_rows, n_cols);
* int[] param_1 = obj.flip();
* obj.reset();
*/
}