Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an m x n binary matrix grid.
A row or column is considered palindromic if its values read the same forward and backward.
You can flip any number of cells in grid from 0 to 1, or from 1 to 0.
Return the minimum number of cells that need to be flipped to make either all rows palindromic or all columns palindromic.
Example 1:
Input: grid = [[1,0,0],[0,0,0],[0,0,1]]
Output: 2
Explanation:
Flipping the highlighted cells makes all the rows palindromic.
Example 2:
Input: grid = [[0,1],[0,1],[0,0]]
Output: 1
Explanation:
Flipping the highlighted cell makes all the columns palindromic.
Example 3:
Input: grid = [[1],[0]]
Output: 0
Explanation:
All rows are already palindromic.
Constraints:
m == grid.lengthn == grid[i].length1 <= m * n <= 2 * 1050 <= grid[i][j] <= 1Problem summary: You are given an m x n binary matrix grid. A row or column is considered palindromic if its values read the same forward and backward. You can flip any number of cells in grid from 0 to 1, or from 1 to 0. Return the minimum number of cells that need to be flipped to make either all rows palindromic or all columns palindromic.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[[1,0,0],[0,0,0],[0,0,1]]
[[0,1],[0,1],[0,0]]
[[1],[0]]
minimum-number-of-moves-to-make-palindrome)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3239: Minimum Number of Flips to Make Binary Grid Palindromic I
class Solution {
public int minFlips(int[][] grid) {
int m = grid.length, n = grid[0].length;
int cnt1 = 0, cnt2 = 0;
for (var row : grid) {
for (int j = 0; j < n / 2; ++j) {
if (row[j] != row[n - j - 1]) {
++cnt1;
}
}
}
for (int j = 0; j < n; ++j) {
for (int i = 0; i < m / 2; ++i) {
if (grid[i][j] != grid[m - i - 1][j]) {
++cnt2;
}
}
}
return Math.min(cnt1, cnt2);
}
}
// Accepted solution for LeetCode #3239: Minimum Number of Flips to Make Binary Grid Palindromic I
func minFlips(grid [][]int) int {
m, n := len(grid), len(grid[0])
cnt1, cnt2 := 0, 0
for _, row := range grid {
for j := 0; j < n/2; j++ {
if row[j] != row[n-j-1] {
cnt1++
}
}
}
for j := 0; j < n; j++ {
for i := 0; i < m/2; i++ {
if grid[i][j] != grid[m-i-1][j] {
cnt2++
}
}
}
return min(cnt1, cnt2)
}
# Accepted solution for LeetCode #3239: Minimum Number of Flips to Make Binary Grid Palindromic I
class Solution:
def minFlips(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
cnt1 = cnt2 = 0
for row in grid:
for j in range(n // 2):
if row[j] != row[n - j - 1]:
cnt1 += 1
for j in range(n):
for i in range(m // 2):
if grid[i][j] != grid[m - i - 1][j]:
cnt2 += 1
return min(cnt1, cnt2)
// Accepted solution for LeetCode #3239: Minimum Number of Flips to Make Binary Grid Palindromic I
/**
* [3239] Minimum Number of Flips to Make Binary Grid Palindromic I
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn min_flips(grid: Vec<Vec<i32>>) -> i32 {
let (m, n) = (grid.len(), grid[0].len());
let mut row = 0;
for i in 0..m {
for j in 0..n / 2 {
if grid[i][j] != grid[i][n - j - 1] {
row += 1;
}
}
}
let mut column = 0;
for i in 0..n {
for j in 0..m / 2 {
if grid[j][i] != grid[m - j - 1][i] {
column += 1;
}
}
}
row.min(column)
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3239() {
assert_eq!(
2,
Solution::min_flips(vec![vec![1, 0, 0], vec![0, 0, 0], vec![0, 0, 1]])
);
assert_eq!(
1,
Solution::min_flips(vec![vec![0, 1], vec![0, 1], vec![0, 0]])
);
}
}
// Accepted solution for LeetCode #3239: Minimum Number of Flips to Make Binary Grid Palindromic I
function minFlips(grid: number[][]): number {
const [m, n] = [grid.length, grid[0].length];
let [cnt1, cnt2] = [0, 0];
for (const row of grid) {
for (let j = 0; j < n / 2; ++j) {
if (row[j] !== row[n - 1 - j]) {
++cnt1;
}
}
}
for (let j = 0; j < n; ++j) {
for (let i = 0; i < m / 2; ++i) {
if (grid[i][j] !== grid[m - 1 - i][j]) {
++cnt2;
}
}
}
return Math.min(cnt1, cnt2);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.