-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efd44e5
commit 9649b7e
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/fishercoder/solutions/fourththousand/_3239.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} | ||
} |