forked from JediXL/LeetCodeByPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path519_random_flip_matrix.py
70 lines (59 loc) · 1.54 KB
/
519_random_flip_matrix.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'''
@auther: Jedi.L
@Date: Wed, Feb 20, 2019 11:44
@Email: [email protected]
'''
import random
# class Solution:
# def __init__(self, n_rows, n_cols):
# """
# :type n_rows: int
# :type n_cols: int
# """
# self.cols = n_cols
# self.end = n_rows * n_cols - 1
# self.hash = {}
# self.start = 0
# def flip(self):
# """
# :rtype: List[int]
# """
# position = random.randint(self.start, self.end)
# res = self.hash.get(position, position)
# self.hash[position] = self.d.get(self.start, self.start)
# self.start += 1
# return divmod(res, self.cols)
# def reset(self):
# """
# :rtype: void
# """
# self.hash = {}
# self.start = 0
class Solution:
def __init__(self, n_rows, n_cols):
"""
:type n_rows: int
:type n_cols: int
"""
self.cols = n_cols
self.end = n_rows * n_cols - 1
self.fliped = set()
self.start = 0
def flip(self):
"""
:rtype: List[int]
"""
while True:
position = random.randint(self.start, self.end)
if position not in self.fliped:
self.fliped.add(position)
return divmod(position, self.cols)
def reset(self):
"""
:rtype: void
"""
self.fliped = set()
# Your Solution object will be instantiated and called as such:
# obj = Solution(n_rows, n_cols)
# param_1 = obj.flip()
# obj.reset()