classSolution:def__init__(self,m:int,n:int):self.m=mself.n=nself.total=m*nself.mp={}defflip(self)->List[int]:self.total-=1x=random.randint(0,self.total)idx=self.mp.get(x,x)self.mp[x]=self.mp.get(self.total,self.total)return[idx//self.n,idx%self.n]defreset(self)->None:self.total=self.m*self.nself.mp.clear()# Your Solution object will be instantiated and called as such:# obj = Solution(m, n)# param_1 = obj.flip()# obj.reset()
classSolution{privateintm;privateintn;privateinttotal;privateRandomrand=newRandom();privateMap<Integer,Integer>mp=newHashMap<>();publicSolution(intm,intn){this.m=m;this.n=n;this.total=m*n;}publicint[]flip(){intx=rand.nextInt(total--);intidx=mp.getOrDefault(x,x);mp.put(x,mp.getOrDefault(total,total));returnnewint[]{idx/n,idx%n};}publicvoidreset(){total=m*n;mp.clear();}}/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(m, n); * int[] param_1 = obj.flip(); * obj.reset(); */