From 9649b7e5d4447d29e79c6986e43dff17b639efdc Mon Sep 17 00:00:00 2001 From: Fisher Coder Date: Sat, 3 Aug 2024 10:31:11 -0700 Subject: [PATCH] add 3239 --- .../algorithms/4th_thousand/README.md | 1 + .../solutions/fourththousand/_3239.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/main/java/com/fishercoder/solutions/fourththousand/_3239.java diff --git a/paginated_contents/algorithms/4th_thousand/README.md b/paginated_contents/algorithms/4th_thousand/README.md index 9e01e495d0..a16663466a 100644 --- a/paginated_contents/algorithms/4th_thousand/README.md +++ b/paginated_contents/algorithms/4th_thousand/README.md @@ -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 diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java new file mode 100644 index 0000000000..f08bc52702 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java @@ -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; + } + } +}