@@ -83,32 +83,245 @@ tags:
83
83
84
84
<!-- solution:start -->
85
85
86
- ### 方法一
86
+ ### 方法一:分类讨论
87
+
88
+ 行和列都是回文的,那么对于任意 $i \in [ 0, m / 2)$ 和 $j \in [ 0, n / 2)$,都需要满足 $\text{grid}[ i] [ j ] = \text{grid}[ m - i - 1] [ j ] = \text{grid}[ i] [ n - j - 1 ] = \text{grid}[ m - i - 1] [ n - j - 1 ] $,要么都变成 $0$,要么都变成 $1$,变成 $0$ 的次数为 $c_0 = \text{grid}[ i] [ j ] + \text{grid}[ m - i - 1] [ j ] + \text{grid}[ i] [ n - j - 1 ] + \text{grid}[ m - i - 1] [ n - j - 1 ] $,变成 $1$ 的次数为 $c_1 = 4 - c_0$,取两者的较小值,累加到答案中。
89
+
90
+ 接下来,我们再讨论 $m$ 和 $n$ 的奇偶性:
91
+
92
+ - 如果 $m$ 和 $n$ 都是偶数,那么直接返回答案;
93
+ - 如果 $m$ 和 $n$ 都是奇数,那么最中间的格子只能是 $0$,因为题目要求 $1$ 的数目可以被 $4$ 整除;
94
+ - 如果 $m$ 是奇数,而 $n$ 是偶数,那么我们需要考虑最中间的一行;
95
+ - 如果 $m$ 是偶数,而 $n$ 是奇数,那么我们需要考虑最中间的一列。
96
+
97
+ 对于后两种情况,我们需要统计最中间的一行或一列中对应位置不相同的格子对数 $\text{diff}$,以及对应位置相同且为 $1$ 的格子个数 $\text{cnt1}$,然后再分情况讨论:
98
+
99
+ - 如果 $\text{cnt1} \bmod 4 = 0$,那么我们只需要将 $\text{diff}$ 个格子变成 $0$ 即可,操作次数为 $\text{diff}$;
100
+ - 否则,说明 $\text{cnt1} = 2$,此时如果 $\text{diff} \lt 0$,我们可以将其中一个格子变成 $1$,使得 $\text{cnt1} = 4$,那么剩下的 $\text{diff} - 1$ 个格子变成 $0$ 即可,操作次数一共为 $\text{diff}$。
101
+ - 否则,如果 $\text{diff} = 0$,我们就把 $\text{2}$ 个格子变成 $0$,使得 $\text{cnt1} \bmod 4 = 0$,操作次数为 $\text{2}$。
102
+
103
+ 我们将操作次数累加到答案中,最后返回答案即可。
104
+
105
+ 时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。
87
106
88
107
<!-- tabs:start -->
89
108
90
109
#### Python3
91
110
92
111
``` python
93
-
112
+ class Solution :
113
+ def minFlips (self , grid : List[List[int ]]) -> int :
114
+ m, n = len (grid), len (grid[0 ])
115
+ ans = 0
116
+ for i in range (m // 2 ):
117
+ for j in range (n // 2 ):
118
+ x, y = m - i - 1 , n - j - 1
119
+ cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]
120
+ ans += min (cnt1, 4 - cnt1)
121
+ if m % 2 and n % 2 :
122
+ ans += grid[m // 2 ][n // 2 ]
123
+ diff = cnt1 = 0
124
+ if m % 2 :
125
+ for j in range (n // 2 ):
126
+ if grid[m // 2 ][j] == grid[m // 2 ][n - j - 1 ]:
127
+ cnt1 += grid[m // 2 ][j] * 2
128
+ else :
129
+ diff += 1
130
+ if n % 2 :
131
+ for i in range (m // 2 ):
132
+ if grid[i][n // 2 ] == grid[m - i - 1 ][n // 2 ]:
133
+ cnt1 += grid[i][n // 2 ] * 2
134
+ else :
135
+ diff += 1
136
+ ans += diff if cnt1 % 4 == 0 or diff else 2
137
+ return ans
94
138
```
95
139
96
140
#### Java
97
141
98
142
``` java
99
-
143
+ class Solution {
144
+ public int minFlips (int [][] grid ) {
145
+ int m = grid. length, n = grid[0 ]. length;
146
+ int ans = 0 ;
147
+ for (int i = 0 ; i < m / 2 ; ++ i) {
148
+ for (int j = 0 ; j < n / 2 ; ++ j) {
149
+ int x = m - i - 1 , y = n - j - 1 ;
150
+ int cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y];
151
+ ans += Math . min(cnt1, 4 - cnt1);
152
+ }
153
+ }
154
+ if (m % 2 == 1 && n % 2 == 1 ) {
155
+ ans += grid[m / 2 ][n / 2 ];
156
+ }
157
+
158
+ int diff = 0 , cnt1 = 0 ;
159
+ if (m % 2 == 1 ) {
160
+ for (int j = 0 ; j < n / 2 ; ++ j) {
161
+ if (grid[m / 2 ][j] == grid[m / 2 ][n - j - 1 ]) {
162
+ cnt1 += grid[m / 2 ][j] * 2 ;
163
+ } else {
164
+ diff += 1 ;
165
+ }
166
+ }
167
+ }
168
+ if (n % 2 == 1 ) {
169
+ for (int i = 0 ; i < m / 2 ; ++ i) {
170
+ if (grid[i][n / 2 ] == grid[m - i - 1 ][n / 2 ]) {
171
+ cnt1 += grid[i][n / 2 ] * 2 ;
172
+ } else {
173
+ diff += 1 ;
174
+ }
175
+ }
176
+ }
177
+ ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2 ;
178
+ return ans;
179
+ }
180
+ }
100
181
```
101
182
102
183
#### C++
103
184
104
185
``` cpp
105
-
186
+ class Solution {
187
+ public:
188
+ int minFlips(vector<vector<int >>& grid) {
189
+ int m = grid.size(), n = grid[ 0] .size();
190
+ int ans = 0;
191
+ for (int i = 0; i < m / 2; ++i) {
192
+ for (int j = 0; j < n / 2; ++j) {
193
+ int x = m - i - 1, y = n - j - 1;
194
+ int cnt1 = grid[ i] [ j ] + grid[ x] [ j ] + grid[ i] [ y ] + grid[ x] [ y ] ;
195
+ ans += min(cnt1, 4 - cnt1);
196
+ }
197
+ }
198
+ if (m % 2 == 1 && n % 2 == 1) {
199
+ ans += grid[ m / 2] [ n / 2 ] ;
200
+ }
201
+
202
+ int diff = 0, cnt1 = 0;
203
+ if (m % 2 == 1) {
204
+ for (int j = 0; j < n / 2; ++j) {
205
+ if (grid[m / 2][j] == grid[m / 2][n - j - 1]) {
206
+ cnt1 += grid[m / 2][j] * 2;
207
+ } else {
208
+ diff += 1;
209
+ }
210
+ }
211
+ }
212
+ if (n % 2 == 1 ) {
213
+ for (int i = 0; i < m / 2; ++i) {
214
+ if (grid[i][n / 2] == grid[m - i - 1][n / 2]) {
215
+ cnt1 += grid[i][n / 2] * 2;
216
+ } else {
217
+ diff += 1;
218
+ }
219
+ }
220
+ }
221
+ ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2;
222
+ return ans;
223
+ }
224
+ };
106
225
```
107
226
108
227
#### Go
109
228
110
229
``` go
230
+ func minFlips (grid [][]int ) int {
231
+ m , n := len (grid), len (grid[0 ])
232
+ ans := 0
233
+
234
+ for i := 0 ; i < m/2 ; i++ {
235
+ for j := 0 ; j < n/2 ; j++ {
236
+ x , y := m-i-1 , n-j-1
237
+ cnt1 := grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]
238
+ ans += min (cnt1, 4 -cnt1)
239
+ }
240
+ }
241
+
242
+ if m%2 == 1 && n%2 == 1 {
243
+ ans += grid[m/2 ][n/2 ]
244
+ }
245
+
246
+ diff , cnt1 := 0 , 0
247
+
248
+ if m%2 == 1 {
249
+ for j := 0 ; j < n/2 ; j++ {
250
+ if grid[m/2 ][j] == grid[m/2 ][n-j-1 ] {
251
+ cnt1 += grid[m/2 ][j] * 2
252
+ } else {
253
+ diff += 1
254
+ }
255
+ }
256
+ }
257
+
258
+ if n%2 == 1 {
259
+ for i := 0 ; i < m/2 ; i++ {
260
+ if grid[i][n/2 ] == grid[m-i-1 ][n/2 ] {
261
+ cnt1 += grid[i][n/2 ] * 2
262
+ } else {
263
+ diff += 1
264
+ }
265
+ }
266
+ }
267
+
268
+ if cnt1%4 == 0 || diff > 0 {
269
+ ans += diff
270
+ } else {
271
+ ans += 2
272
+ }
273
+
274
+ return ans
275
+ }
276
+ ```
111
277
278
+ #### TypeScript
279
+
280
+ ``` ts
281
+ function minFlips(grid : number [][]): number {
282
+ const m = grid .length ;
283
+ const n = grid [0 ].length ;
284
+ let ans = 0 ;
285
+
286
+ for (let i = 0 ; i < Math .floor (m / 2 ); i ++ ) {
287
+ for (let j = 0 ; j < Math .floor (n / 2 ); j ++ ) {
288
+ const x = m - i - 1 ;
289
+ const y = n - j - 1 ;
290
+ const cnt1 = grid [i ][j ] + grid [x ][j ] + grid [i ][y ] + grid [x ][y ];
291
+ ans += Math .min (cnt1 , 4 - cnt1 );
292
+ }
293
+ }
294
+
295
+ if (m % 2 === 1 && n % 2 === 1 ) {
296
+ ans += grid [Math .floor (m / 2 )][Math .floor (n / 2 )];
297
+ }
298
+
299
+ let diff = 0 ,
300
+ cnt1 = 0 ;
301
+
302
+ if (m % 2 === 1 ) {
303
+ for (let j = 0 ; j < Math .floor (n / 2 ); j ++ ) {
304
+ if (grid [Math .floor (m / 2 )][j ] === grid [Math .floor (m / 2 )][n - j - 1 ]) {
305
+ cnt1 += grid [Math .floor (m / 2 )][j ] * 2 ;
306
+ } else {
307
+ diff += 1 ;
308
+ }
309
+ }
310
+ }
311
+
312
+ if (n % 2 === 1 ) {
313
+ for (let i = 0 ; i < Math .floor (m / 2 ); i ++ ) {
314
+ if (grid [i ][Math .floor (n / 2 )] === grid [m - i - 1 ][Math .floor (n / 2 )]) {
315
+ cnt1 += grid [i ][Math .floor (n / 2 )] * 2 ;
316
+ } else {
317
+ diff += 1 ;
318
+ }
319
+ }
320
+ }
321
+
322
+ ans += cnt1 % 4 === 0 || diff > 0 ? diff : 2 ;
323
+ return ans ;
324
+ }
112
325
```
113
326
114
327
<!-- tabs:end -->
0 commit comments