519. Random Flip Matrix #
Problem #
There is an m x n binary grid matrix with all the values set 0 initially. Design an algorithm to randomly pick an index (i, j) where matrix[i][j] == 0 and flips it to 1. All the indices (i, j) where matrix[i][j] == 0 should be equally likely to be returned.
Optimize your algorithm to minimize the number of calls made to the built-in random function of your language and optimize the time and space complexity.
Implement the Solution class:
- Solution(int m, int n) Initializes the object with the size of the binary matrix m and n.
- int[] flip() Returns a random index [i, j] of the matrix where matrix[i][j] == 0 and flips it to 1.
- void reset() Resets all the values of the matrix to be 0.
Example 1:
Input
["Solution", "flip", "flip", "flip", "reset", "flip"]
[[3, 1], [], [], [], [], []]
Output
[null, [1, 0], [2, 0], [0, 0], null, [2, 0]]
Explanation
Solution solution = new Solution(3, 1);
solution.flip(); // return [1, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.
solution.flip(); // return [2, 0], Since [1,0] was returned, [2,0] and [0,0]
solution.flip(); // return [0, 0], Based on the previously returned indices, only [0,0] can be returned.
solution.reset(); // All the values are reset to 0 and can be returned.
solution.flip(); // return [2, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.
Constraints:
- 1 <= m, n <= 10000
- There will be at least one free cell for each call to flip.
- At most 1000 calls will be made to flip and reset.
Problem Summary #
You are given an m x n binary matrix matrix, and all values are initialized to 0. Design an algorithm to randomly select an index (i, j) such that matrix[i][j] == 0, and change its value to 1. All indices (i, j) that satisfy matrix[i][j] == 0 should have an equal probability of being selected.
Minimize the number of calls to the built-in random function as much as possible, and optimize time and space complexity.
Implement the Solution class:
- Solution(int m, int n) Initializes the object using the size m and n of the binary matrix
- int[] flip() Returns a random index [i, j] such that matrix[i][j] == 0, and changes the value in the corresponding cell to 1
- void reset() Resets all values in the matrix to 0
Solution Approach #
- Convert the two-dimensional matrix into one dimension using a hash table. Each time, randomly select any element in the one-dimensional representation, then swap it with the last element, and reduce the total number of one-dimensional elements by one
- The default mapping in the hash table is x->x, and then store the special key-value pairs that do not satisfy this mapping in the hash table
Code #
package leetcode
import "math/rand"
type Solution struct {
r int
c int
total int
mp map[int]int
}
func Constructor(m int, n int) Solution {
return Solution{
r: m,
c: n,
total: m * n,
mp: map[int]int{},
}
}
func (this *Solution) Flip() []int {
k := rand.Intn(this.total)
val := k
if v, ok := this.mp[k]; ok {
val = v
}
if _, ok := this.mp[this.total-1]; ok {
this.mp[k] = this.mp[this.total-1]
} else {
this.mp[k] = this.total - 1
}
delete(this.mp, this.total - 1)
this.total--
newR, newC := val/this.c, val%this.c
return []int{newR, newC}
}
func (this *Solution) Reset() {
this.total = this.r * this.c
this.mp = map[int]int{}
}