Leetcode 913 Solution

This article provides solution to leetcode question 913 (random-flip-matrix)

https://leetcode.com/problems/random-flip-matrix

Solution

class Solution:

    def __init__(self, m: int, n: int):
        self.m = m
        self.n = n
        self.size = m * n
        self.mapping = {}

    def flip(self) -> List[int]:
        index = randint(0, self.size - 1)

        if index in self.mapping:
            real_index = self.mapping[index]
        else:
            real_index = index

        self.size -= 1
        self.mapping[index] = self.mapping.get(self.size, self.size)

        return real_index // self.n, real_index % self.n

    def reset(self) -> None:
        self.size = self.m * self.n
        self.mapping = {}


# Your Solution object will be instantiated and called as such:
# obj = Solution(m, n)
# param_1 = obj.flip()
# obj.reset()