3240. 最少翻转次数使二进制矩阵回文 II
为保证权益,题目请参考 3240. 最少翻转次数使二进制矩阵回文 II(From LeetCode).
解决方案1
Python
python
from itertools import product
from typing import List
class Solution:
def minFlips(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
ans = 0
one_one_count = 0
one_zero_count = 0
for i, j in product(range((m + 1) // 2), range((n + 1) // 2)):
if i == m - 1 - i and j == n - 1 - j:
if grid[i][j] == 1:
ans += 1
break
if i == m - 1 - i:
vs = [grid[i][j], grid[i][n - 1 - j]]
one_count = sum(vs)
zero_cont = 2 - one_count
if one_count == 0:
ans += 0
elif one_count == 1:
ans += 1
one_zero_count += 1
else:
ans += 0
one_one_count += 2
continue
if j == n - 1 - j:
vs = [grid[i][j], grid[m - 1 - i][j]]
one_count = sum(vs)
zero_cont = 2 - one_count
if one_count == 0:
ans += 0
elif one_count == 1:
ans += 1
one_zero_count += 1
else:
ans += 0
one_one_count += 2
continue
one_count = sum(
[
grid[i][j],
grid[i][n - 1 - j],
grid[m - 1 - i][j],
grid[m - 1 - i][n - 1 - j],
]
)
ans += min(one_count, 4 - one_count)
if one_one_count % 4 != 0 and one_zero_count == 0:
ans += 2
return ans
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
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