Minimum Number of Flips to Make Binary Grid Palindromic I - Problem

You are given an m x n binary matrix grid where each cell contains either 0 or 1. Your goal is to make the entire grid follow a palindromic pattern by flipping the minimum number of cells.

A palindromic pattern means that either:

  • All rows are palindromic (each row reads the same forwards and backwards), OR
  • All columns are palindromic (each column reads the same top-to-bottom and bottom-to-top)

You can flip any cell from 0 to 1 or from 1 to 0. Return the minimum number of flips needed to achieve one of these two palindromic arrangements.

Example: For matrix [[1,0,0],[0,1,0],[1,1,1]], you need to choose between making all rows palindromic or all columns palindromic, whichever requires fewer flips.

Input & Output

example_1.py โ€” Basic Grid
$ Input: grid = [[1,0,0],[0,1,0],[1,1,1]]
โ€บ Output: 1
๐Ÿ’ก Note: To make all rows palindromic: Row 1 needs 1 flip (1,0,0 โ†’ 1,0,1 or 0,0,0), Row 2 is already palindromic, Row 3 is already palindromic. Total: 1 flip. To make all columns palindromic: Column 1 needs 0 flips (already palindromic), Column 2 needs 1 flip, Column 3 needs 0 flips. Total: 1 flip. Minimum is 1.
example_2.py โ€” Single Row
$ Input: grid = [[1,0,1,0,1]]
โ€บ Output: 1
๐Ÿ’ก Note: For a single row grid, we compare row vs column costs. Row: 1 flip needed to make [1,0,1,0,1] palindromic. Columns: Each column has only 1 element (already palindromic), so 0 flips total. Minimum is 0 flips (choose columns).
example_3.py โ€” Already Palindromic
$ Input: grid = [[1,0,1],[0,1,0],[1,0,1]]
โ€บ Output: 0
๐Ÿ’ก Note: This grid is already palindromic both row-wise and column-wise. Row analysis: all rows are palindromes (0 flips). Column analysis: all columns are palindromes (0 flips). Minimum is 0.

Visualization

Tap to expand
Row Strategy100Row 1: 1โ†’0 or 0โ†’1 (1 flip)Row 2: Already palindromicRow 3: Already palindromicTotal Row Flips: 3Column Strategy101Col 1: 1,0,1 palindromicCol 2: 0,1,1 (1 flip needed)Col 3: 0,0,1 palindromicTotal Column Flips: 1Optimal Choicemin(3 row flips, 1 column flip) = 1Choose column strategy!
Understanding the Visualization
1
Analyze Rows
Count how many flips needed to make each row read the same forwards and backwards
2
Analyze Columns
Count how many flips needed to make each column read the same top-to-bottom and bottom-to-top
3
Choose Strategy
Pick the approach (row or column) that requires fewer total flips
4
Return Result
Output the minimum number of flips needed
Key Takeaway
๐ŸŽฏ Key Insight: We don't need to find the globally optimal solution - just compare two strategies (all rows vs all columns palindromic) and pick the cheaper one!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m*n)

We visit each cell exactly twice - once during row analysis and once during column analysis

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for pointer variables and counters

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค m, n โ‰ค 300
  • grid[i][j] is either 0 or 1
  • Time limit: 2 seconds
  • Space limit: 256 MB
Asked in
Google 28 Amazon 22 Meta 15 Microsoft 12
28.4K Views
Medium Frequency
~15 min Avg. Time
945 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen