- 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
packagecom.fishercoder.solutions.fourththousand;
publicclass_3239 {
publicstaticclassSolution1 {
publicintminFlips(int[][] grid) {
intm = grid.length;
intn = grid[0].length;
intans = m * n;
// try rows first
intflips = 0;
for (inti = 0; i < m; i++) {
for (intleft = 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 (intj = 0; j < n; j++) {
for (inttop = 0, bottom = m - 1; top < bottom; top++, bottom--) {
if (grid[top][j] != grid[bottom][j]) {
flips++;
}
}
}
ans = Math.min(flips, ans);
returnans;
}
}
}