Beats 93% Python
Intuition\nIn stream processing of random numbers. Much like shuffling, you replace the self.start number with the random index you picked. \n\nIn this implemen
# Intuition\nIn stream processing of random numbers. Much like shuffling, you replace the self.start number with the random index you picked. \n\nIn this implementation, you are randomly picking from an incrementally smaller pool of unpicked numbers. The picked random number, you put in self.start so that it is no longer picked. You replace it with the self.start index so that it can be still in the pool of being picked.\n\nYou would shuffle by also storing random pick in self.start, but because you are moving on, that portion is not necessary.\n\nself.mydict[my_random_pick] = self.mydict.get(self.start, self.start)\n \nIn the dictionary, the dictionary key represents the original index and the dictionary value represents the shuffled position.\n\nThis comes from the Fisher Yates Shuffle\n\n1. Write down the numbers from 1 through N.\n2. Pick a random number k between one and the number of unstruck numbers remaining (inclusive).\n3. Counting from the low end, strike out the kth number not yet struck out, and write it down at the end of a separate list.\n4. Repeat from step 2 until all the numbers have been struck out.\n5. The sequence of numbers written down in step 3 is now a random permutation of the original numbers.\n\n\n# Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(Q)\nWhen Q is number of queries.\n\n# Code\n```\nimport random\n\nclass Solution:\n\n    def __init__(self, m: int, n: int):\n        self.n_cols = n\n        self.start = 0\n        self.end = m * n - 1\n        self.mydict = {}        \n\n    def flip(self) -> List[int]:\n        my_random_pick = random.randint(self.start, self.end)\n        # self.start is replaced in case that index was already picked and shuffled.\n        result = self.mydict.get(my_random_pick, my_random_pick)\n        self.mydict[my_random_pick] = self.mydict.get(self.start, self.start)\n        self.start += 1\n\n        return (result // self.n_cols, result % self.n_cols)        \n\n    def reset(self) -> None:\n        self.mydict = {}\n        self.start = 0\n        \n\n\n# Your Solution object will be instantiated and called as such:\n# obj = Solution(m, n)\n# param_1 = obj.flip()\n# obj.reset()\n```
['Python3']