Talentd
FleetCode

3240. Minimum Number of Flips to Make Binary Grid Palindromic II

Medium25.5% acceptance1 company

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:

Input: grid = [[1,0,0],[0,1,0],[0,0,1]]

Output: 3

Explanation:

Example 2:

Input: grid = [[0,1],[0,1],[0,0]]

Output: 2

Explanation:

Example 3:

Input: grid = [[1],[1]]

Output: 2

Explanation:

Constraints:

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

Asked at 1 company

Hints open one at a time

Hint 1
For each (x, y), find (m - 1 - x, y), (m - 1 - x, n - 1 - y), and (x, n - 1 - y); they should be the same.
Hint 2
Note that we need to specially handle the middle row (column) if the number of rows (columns) is odd.
19:47
Still stuck?Read 5 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minFlips(self, grid: List[List[int]]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[[1,0,0],[0,1,0],[0,0,1]]
Input
[[0,1],[0,1],[0,0]]
Input
[[1],[1]]
Your code is saved on this device.Solution