Random Flip Matrix
Complexity
Time complexity: O(1)

Space complexity: O(K)
Code


# Complexity
- Time complexity: O(1)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(K)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python []
import random

class Solution(object):

    def __init__(self, m, n):
        """
        :type m: int
        :type n: int
        """
        self.m = m
        self.n = n
        self.total = m * n
        self.map = {}

    def flip(self):
        """
        :rtype: List[int]
        """
        rand_index = random.randint(0, self.total - 1)
        
        x = self.map.get(rand_index, rand_index)
        
        self.map[rand_index] = self.map.get(self.total - 1, self.total - 1)
        
        self.total -= 1
        
        return [x // self.n, x % self.n]

    def reset(self):
        """
        :rtype: None
        """
        self.total = self.m * self.n
        self.map.clear()



# Your Solution object will be instantiated and called as such:
# obj = Solution(m, n)
# param_1 = obj.flip()
# obj.reset()
```
['Hash Table', 'Math', 'Reservoir Sampling', 'Randomized', 'Python']