You is given the number of rows and number of n_rows
columns of n_cols
a 2D binary matrix where all values is 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 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
and0 <= col.id < n_cols
flip
Won't is called when the matrix have no 0 values left.
- The total number of calls to and would not
flip
reset
exceed 1000.
Example 1:
["Solution", "Flip", "Flip", "Flip", "Flip") [null,[0,1],[1,2],[1,0],[1,1]]
Example 2:
["Solution", "Flip", "Flip", "Reset", "Flip") [null,[0,0],[0,1],null,[0,0]]
Explanation of Input Syntax:
The input is a lists:the subroutines called and their arguments. Solution
' s constructor has a arguments, and. and has n_rows
n_cols
flip
reset
no arguments. Arguments is always wrapped with a list, even if there aren ' t.
S
[Leetcode] Random flip matrix randomly flipped matrices