diff --git a/leetcode/0519.Random-Flip-Matrix/519.Random Flip Matrix.go b/leetcode/0519.Random-Flip-Matrix/519.Random Flip Matrix.go new file mode 100644 index 00000000..4b0df378 --- /dev/null +++ b/leetcode/0519.Random-Flip-Matrix/519.Random Flip Matrix.go @@ -0,0 +1,43 @@ +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{} +}