-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3239.java
32 lines (31 loc) · 1 KB
/
_3239.java
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
package com.fishercoder.solutions.fourththousand;
public class _3239 {
public static class Solution1 {
public int minFlips(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int ans = m * n;
// try rows first
int flips = 0;
for (int i = 0; i < m; i++) {
for (int left = 0, right = n - 1; left < right; left++, right--) {
if (grid[i][left] != grid[i][right]) {
flips++;
}
}
}
ans = Math.min(ans, flips);
flips = 0;
// try columns now
for (int j = 0; j < n; j++) {
for (int top = 0, bottom = m - 1; top < bottom; top++, bottom--) {
if (grid[top][j] != grid[bottom][j]) {
flips++;
}
}
}
ans = Math.min(flips, ans);
return ans;
}
}
}