Watch 7 video solutions for Minimum Number of Flips to Make Binary Grid Palindromic I, a medium level problem involving Array, Two Pointers, Matrix. This walkthrough by codi has 1,252 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 Overview: You are given a binary matrix. A row or column is palindromic if reading it from left-to-right equals right-to-left. The task is to compute the minimum number of cell flips needed so that either every row becomes palindromic or every column becomes palindromic.
Approach 1: Greedy Row/Column Two-Pointer Scan (O(m*n) time, O(1) space)
The grid can be fixed in two independent ways: make all rows palindromic or make all columns palindromic. For rows, iterate each row and use the classic two pointers technique. Start one pointer at the left and one at the right. If grid[r][l] and grid[r][r] differ, one flip is required to make the pair equal. Count mismatches for every symmetric pair and sum them across rows. The same idea applies to columns: scan each column with pointers from top and bottom and count mismatched pairs.
The key observation: every symmetric pair must contain identical values for the row or column to be palindromic. Flipping either cell resolves the mismatch, so each mismatch contributes exactly one flip. Compute the total flips needed for rows and the total for columns, then return the smaller value. This greedy counting works because each pair is independent and there is no cross-pair dependency.
This method uses only simple iteration over the array structure representing the grid. No extra memory or preprocessing is required, which keeps the implementation short and efficient.
Approach 2: Dynamic Programming Pair Aggregation (O(m*n) time, O(1) space)
A DP-style interpretation treats every symmetric pair as a small subproblem. For a row, the pair (i, j) and (i, n-1-j) must end with the same value. The minimum flips for the pair is 0 if the values already match and 1 otherwise. A DP transition simply accumulates the optimal cost for each processed pair. Repeating this across all rows produces the total row cost; repeating across columns produces the column cost.
Although labeled as dynamic programming, the state transition collapses to a direct mismatch count because each pair decision is independent. The DP framing can still help when reasoning about extensions of the problem (for example when flips have different costs or when multiple constraints interact).
Recommended for interviews: The greedy two-pointer approach is the expected solution. It demonstrates clear reasoning about symmetry and efficient traversal of a matrix. A brute-force mindset would attempt flipping cells and rechecking palindromes, but recognizing that only symmetric pairs matter reduces the problem to a linear scan. Interviewers typically look for this observation and an O(m*n) implementation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Two-Pointer Scan (Rows vs Columns) | O(m*n) | O(1) | Best general solution. Directly counts mismatched symmetric pairs in rows and columns. |
| Dynamic Programming Pair Aggregation | O(m*n) | O(1) | Useful for reasoning about symmetric pair costs or when extending the problem with additional constraints. |