forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-flip-matrix.py
More file actions
41 lines (30 loc) · 845 Bytes
/
random-flip-matrix.py
File metadata and controls
41 lines (30 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Time: ctor: O(1)
# flip: O(1)
# reset: O(min(f, r * c))
# Space: O(min(f, r * c))
import random
class Solution(object):
def __init__(self, n_rows, n_cols):
"""
:type n_rows: int
:type n_cols: int
"""
self.__n_rows = n_rows
self.__n_cols = n_cols
self.__n = n_rows*n_cols
self.__lookup = {}
def flip(self):
"""
:rtype: List[int]
"""
self.__n -= 1
target = random.randint(0, self.__n)
x = self.__lookup.get(target, target)
self.__lookup[target] = self.__lookup.get(self.__n, self.__n)
return divmod(x, self.__n_cols)
def reset(self):
"""
:rtype: void
"""
self.__n = self.__n_rows*self.__n_cols
self.__lookup = {}