File tree 1 file changed +5
-4
lines changed
scripts/algorithms/R/Random Flip Matrix
1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 45 ms (Top 62.26%) | Memory: 50.7 MB (Top 63.21%)
1
2
// Swap Tail Element Solution
2
3
// 1. Get a random number between [0, size-1]
3
4
// 2. size - 1
@@ -10,7 +11,7 @@ class Solution {
10
11
private int size ;
11
12
private Random random ;
12
13
private Map <Integer , Integer > map ;
13
-
14
+
14
15
public Solution (int m , int n ) {
15
16
M = m ;
16
17
N = n ;
@@ -19,7 +20,7 @@ public Solution(int m, int n) {
19
20
random = new Random ();
20
21
map = new HashMap <>();
21
22
}
22
-
23
+
23
24
public int [] flip () {
24
25
if (size <= 0 ) return new int []{-1 , -1 }; // or throw exception.
25
26
Integer rand = random .nextInt (size );
@@ -29,7 +30,7 @@ public int[] flip() {
29
30
map .put (rand , tail );
30
31
return new int []{idx / N , idx % N };
31
32
}
32
-
33
+
33
34
public void reset () {
34
35
map = new HashMap ();
35
36
size = CAPACITY ;
@@ -41,4 +42,4 @@ public void reset() {
41
42
* Solution obj = new Solution(m, n);
42
43
* int[] param_1 = obj.flip();
43
44
* obj.reset();
44
- */
45
+ */
You can’t perform that action at this time.
0 commit comments