-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh1284.py
58 lines (48 loc) · 2.04 KB
/
h1284.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
class Solution:
def minFlips(self, mat: List[List[int]]) -> int:
self.visited = {}
self.case(mat, sum([sum(x) for x in mat]), 0)
return self.visited.get(('0' * len(mat) * len(mat[0])), -1)
def case(self, mat: List[List[int]], ones: int, steps: int) -> bool:
stringify = self.toString(mat)
if not ones :
self.visited[stringify] = min(self.visited.get(stringify, inf), steps)
return True
if stringify in self.visited :
if self.visited.get(stringify) < steps :
return -1
self.visited[stringify] = steps
steps += 1
for i in range(len(mat)) :
for j in range(len(mat[0])) :
tempVal = self.flip(mat, i, j)
tempVal = self.case(mat, ones + tempVal, steps)
self.flip(mat, i, j)
if tempVal:
return False # zeros found so any more steps than this will not improve #steps
return False
def flip(self, mat: List[List[int]], x: int, y: int) -> int : # ret # of new 1's
opposite = {0: 1, 1: 0}
newOnes, newZeros = 0, 0
mat[x][y] = opposite[mat[x][y]]
newOnes += mat[x][y]
newZeros += opposite[mat[x][y]]
if x > 0 :
mat[x - 1][y] = opposite[mat[x - 1][y]]
newOnes += mat[x - 1][y]
newZeros += opposite[mat[x - 1][y]]
if x < len(mat) - 1 :
mat[x + 1][y] = opposite[mat[x + 1][y]]
newOnes += mat[x + 1][y]
newZeros += opposite[mat[x + 1][y]]
if y > 0 :
mat[x][y - 1] = opposite[mat[x][y - 1]]
newOnes += mat[x][y - 1]
newZeros += opposite[mat[x][y - 1]]
if y < len(mat[0]) - 1 :
mat[x][y + 1] = opposite[mat[x][y + 1]]
newOnes += mat[x][y + 1]
newZeros += opposite[mat[x][y + 1]]
return newOnes - newZeros
def toString(self, mat: List[List[int]]) -> str :
return ''.join(''.join([str(y) for y in row]) for row in mat)