-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path519-RandomFlipMatrix.go
More file actions
85 lines (73 loc) · 3.08 KB
/
519-RandomFlipMatrix.go
File metadata and controls
85 lines (73 loc) · 3.08 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
package main
// 519. Random Flip Matrix
// 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 <= 10^4
// There will be at least one free cell for each call to flip.
// At most 1000 calls will be made to flip and reset.
import "fmt"
import "math/rand"
type Solution struct {
used map[int]bool // 记录出现的值
limit, n int
}
func Constructor(m int, n int) Solution {
return Solution{make(map[int]bool), m * n, n}
}
func (this *Solution) Flip() []int {
key := rand.Intn(this.limit)
for this.used[key] { // 需要获取一个未出现的
key = rand.Intn(this.limit)
}
this.used[key] = true
return []int{ key / this.n, key % this.n }
}
func (this *Solution) Reset() {
this.used = make(map[int]bool)
}
/**
* Your Solution object will be instantiated and called as such:
* obj := Constructor(m, n);
* param_1 := obj.Flip();
* obj.Reset();
*/
func main() {
// Solution solution = new Solution(3, 1);
obj := Constructor(3, 1)
fmt.Println(obj)
// solution.flip(); // return [1, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.
fmt.Println(obj.Flip())
fmt.Println(obj)
// solution.flip(); // return [2, 0], Since [1,0] was returned, [2,0] and [0,0]
fmt.Println(obj.Flip())
fmt.Println(obj)
// solution.flip(); // return [0, 0], Based on the previously returned indices, only [0,0] can be returned.
fmt.Println(obj.Flip())
fmt.Println(obj)
// solution.reset(); // All the values are reset to 0 and can be returned.
obj.Reset()
fmt.Println(obj)
// solution.flip(); // return [2, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.
fmt.Println(obj.Flip())
fmt.Println(obj)
}