Minimum Number of Flips to Make Binary Grid Palindromic II

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 all rows and columns palindromic, and the total number of 1's in grid divisible by 4.

Example 1
1 0 0
0 1 0
0 0 1
Inputgrid = [[1,0,0],[0,1,0],[0,0,1]]
Output3
Flipping 3 cells can make every row and column palindromic while making the total number of 1's divisible by 4.
Example 2
0 1
0 1
0 0
Inputgrid = [[0,1],[0,1],[0,0]]
Output2
Flipping 2 cells can make every row and column palindromic while making the total number of 1's divisible by 4.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m * n <= 2 * 10^5
  • 0 <= grid[i][j] <= 1

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate