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).
Input: grid =[[1,0,2],[1,0,2]]Output: 0Explanation:
****
All the cells in the matrix already satisfy the properties.
Input: grid =[[1,1,1],[0,0,0]]Output: 3Explanation:
****
The matrix becomes `[[1,0,1],[1,0,1]]` which satisfies the properties, by
doing these 3 operations:* Change `grid[1][0]` to 1.* Change `grid[0][1]` to 0.* Change `grid[1][2]` to 1.
Input: grid =[[1],[2],[3]]Output: 2Explanation:

There is a single column. We can change the value to 1in each cell using 2operations.
Each column must be uniform (all cells equal), and adjacent columns must differ. This is a coloring problem: assign each column one value, ensuring adjacent columns differ, and minimize changes.
#include<vector>#include<algorithm>usingnamespace std;
classSolution {
public:int minimumOperations(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> cnt(n, vector<int>(10, 0));
for (int j =0; j < n; ++j)
for (int i =0; i < m; ++i)
cnt[j][grid[i][j]]++;
vector<vector<int>> dp(n, vector<int>(10, 1e9));
for (int v =0; v <10; ++v)
dp[0][v] = m - cnt[0][v];
for (int j =1; j < n; ++j) {
for (int v =0; v <10; ++v) {
for (int u =0; u <10; ++u) {
if (u == v) continue;
dp[j][v] = min(dp[j][v], dp[j-1][u] + m - cnt[j][v]);
}
}
}
return*min_element(dp[n-1].begin(), dp[n-1].end());
}
};
import java.util.*;
classSolution {
publicintminimumOperations(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] cnt =newint[n][10];
for (int j = 0; j < n; ++j)
for (int i = 0; i < m; ++i)
cnt[j][grid[i][j]]++;
int[][] dp =newint[n][10];
for (int[] d : dp) Arrays.fill(d, Integer.MAX_VALUE);
for (int v = 0; v < 10; ++v)
dp[0][v]= m - cnt[0][v];
for (int j = 1; j < n; ++j) {
for (int v = 0; v < 10; ++v) {
for (int u = 0; u < 10; ++u) {
if (u == v) continue;
dp[j][v]= Math.min(dp[j][v], dp[j-1][u]+ m - cnt[j][v]);
}
}
}
int res = Integer.MAX_VALUE;
for (int v = 0; v < 10; ++v)
res = Math.min(res, dp[n-1][v]);
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funminimumOperations(grid: Array<IntArray>): Int {
val m = grid.size; val n = grid[0].size
val cnt = Array(n) { IntArray(10) }
for (j in0 until n) for (i in0 until m) cnt[j][grid[i][j]]++val dp = Array(n) { IntArray(10) { Int.MAX_VALUE } }
for (v in0..9) dp[0][v] = m - cnt[0][v]
for (j in1 until n) {
for (v in0..9) {
for (u in0..9) {
if (u == v) continue dp[j][v] = minOf(dp[j][v], dp[j-1][u] + m - cnt[j][v])
}
}
}
return dp[n-1].minOrNull()!! }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defminimumOperations(self, grid: list[list[int]]) -> int:
m, n = len(grid), len(grid[0])
cnt = [[0]*10for _ in range(n)]
for j in range(n):
for i in range(m):
cnt[j][grid[i][j]] +=1 dp = [[float('inf')]*10for _ in range(n)]
for v in range(10):
dp[0][v] = m - cnt[0][v]
for j in range(1, n):
for v in range(10):
for u in range(10):
if u == v: continue dp[j][v] = min(dp[j][v], dp[j-1][u] + m - cnt[j][v])
return min(dp[n-1])
impl Solution {
pubfnminimum_operations(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
letmut cnt =vec![vec![0; 10]; n];
for j in0..n {
for i in0..m {
cnt[j][grid[i][j] asusize] +=1;
}
}
letmut dp =vec![vec![i32::MAX; 10]; n];
for v in0..10 {
dp[0][v] = m asi32- cnt[0][v];
}
for j in1..n {
for v in0..10 {
for u in0..10 {
if u == v { continue; }
dp[j][v] = dp[j][v].min(dp[j-1][u] + m asi32- cnt[j][v]);
}
}
}
*dp[n-1].iter().min().unwrap()
}
}