Minimum Number of Operations to Satisfy Conditions

You are given a 2D matrix grid of size m x n. In one operation, you can change the value of any cell to any non-negative number.

You need to perform some operations such that each cell grid[i][j] is:

  • Equal to the cell below it, i.e. grid[i][j] == grid[i + 1][j] if it exists.
  • Different from the cell to its right, i.e. grid[i][j] != grid[i][j + 1] if it exists.

Return the minimum number of operations needed.

Example 1
1 0 2
1 0 2
Inputgrid = [[1,0,2],[1,0,2]]
Output0
All the cells in the matrix already satisfy the properties.
Example 2
1 1 1
0 0 0
Inputgrid = [[1,1,1],[0,0,0]]
Output3
The matrix can become [[1,0,1],[1,0,1]], which satisfies the properties, by doing 3 operations.

Constraints

  • 1 <= n, m <= 1000
  • 0 <= grid[i][j] <= 9

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