Minimum Number of Flips to Make Binary Grid Palindromic I Problem
#3239
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3239.
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 * 10^50 <= grid[i][j] <= 1
Solution
Approach: Counting
Count the flips needed to make every row a palindrome (for each row, the mismatching pairs grid[i][j] and grid[i][n - 1 - j]), and separately the flips needed to make every column a palindrome. Each mismatching pair needs one flip. Return the smaller of the two counts.
Complexity: O(mn) time, O(1) space, where m and n are the numbers of rows and columns.
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)