Skip to content

Commit

Permalink
add 3239
Browse files Browse the repository at this point in the history
  • Loading branch information
fishercoder1534 committed Aug 3, 2024
1 parent efd44e5 commit 9649b7e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions paginated_contents/algorithms/4th_thousand/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
| # | Title | Solutions | Video | Difficulty | Tag
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
| 3239 | [Minimum Number of Flips to Make Binary Grid Palindromic I](https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java) | | Easy |
| 3238 | [Find the Number of Winning Players](https://leetcode.com/problems/find-the-number-of-winning-players/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3238.java) | | Easy |
| 3237 | [Alt and Tab Simulation](https://leetcode.com/problems/alt-and-tab-simulation/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3237.java) | | Medium |
| 3234 | [Count the Number of Substrings With Dominant Ones](https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3234.java) | | Medium | Sliding Window
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/fishercoder/solutions/fourththousand/_3239.java
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;
}
}
}

0 comments on commit 9649b7e

Please sign in to comment.