Python solution that runs faster than 100% other submissions
The basic idea is to swap the sampled index with the last index and then reduce the sampling range by 1. To aovid the usage expensive list(range(m*n)), we use a
The basic idea is to swap the sampled index with the last index and then reduce the sampling range by 1. To aovid the usage expensive ```list(range(m*n))```, we use a hash table to save the last element, i.e. the key is the sampled index and the value is the last index of that sampling. The time complexity is always O(1) and space complexity is O(k) where k is the time of flips after reset.\n\nThe reference is as following:\nhttps://medium.com/@yanshuairyan/leetcode-519-random-flip-matrix-6d3177fff1fc\n\n```\nimport random\nclass Solution:\n\n    def __init__(self, m: int, n: int):\n        self.m = m\n        self.n = n\n        self.reset()\n\n    def flip(self) -> List[int]:\n        # hash tabled based, time: O(1), space: O(k) where k is time of flips after reset\n        \n        r_int = int(random.random()*self.ctr)\n        \n        # check if this sampled index point to a last index \n        res = self.ht.get(r_int, r_int)\n        \n        # save last index in hash table \n        # this is identical to swap this index with last one, but it is more space efficient\n        self.ht[r_int] = self.ht.get(self.ctr-1, self.ctr-1)\n        \n        # shrink sampling range each time\n        self.ctr-=1\n        \n        return [res//self.n, res%self.n]\n\n    def reset(self) -> None:\n        self.ht = dict()\n        self.ctr = self.m*self.n\n```
[]