You are given a 0-indexedn x n grid where n is odd, and grid[r][c]
is 0, 1, or 2.
We say that a cell belongs to the Letter Y if it belongs to one of the following:
The diagonal starting at the top-left cell and ending at the center cell of the grid.
The diagonal starting at the top-right cell and ending at the center cell of the grid.
The vertical line starting at the center cell and ending at the bottom border of the grid.
The Letter Y is written on the grid if and only if:
All values at cells belonging to the Y are equal.
All values at cells not belonging to the Y are equal.
The values at cells belonging to the Y are different from the values at cells not belonging to the Y.
Return theminimum number of operations needed to write the letter Y on the grid given that in one operation you can change the value at any cell to0,1,or2.

Input: grid =[[1,2,2],[1,1,0],[0,1,0]]Output: 3Explanation: We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 1while those that do not belong to Y are equal to 0.It can be shown that 3is the minimum number of operations needed to write Y on the grid.

Input: grid =[[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]]Output: 12Explanation: We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 0while those that do not belong to Y are equal to 2.It can be shown that 12is the minimum number of operations needed to write Y on the grid.
For each possible value for Y and non-Y cells (must be different), count the number of changes needed to make all Y cells the same and all non-Y cells the same. Try all possible value pairs (0,1), (0,2), (1,0), (1,2), (2,0), (2,1).
classSolution {
funminimumOperations(grid: Array<IntArray>): Int {
val n = grid.size
val c = n/2val isY = Array(n) { BooleanArray(n) }
for (i in0 until n) { isY[i][i] = true; isY[i][n-1-i] = true }
for (i in c until n) isY[i][c] = truevar res = n*n
for (yVal in0..2) {
for (nonYVal in0..2) {
if (yVal == nonYVal) continuevar changes = 0for (i in0 until n) {
for (j in0 until n) {
if (isY[i][j] && grid[i][j] != yVal) changes++if (!isY[i][j] && grid[i][j] != nonYVal) changes++ }
}
res = minOf(res, changes)
}
}
return res
}
}
classSolution:
defminimumOperations(self, grid: list[list[int]]) -> int:
n = len(grid)
c = n//2 isY = [[False]*n for _ in range(n)]
for i in range(n):
isY[i][i] = isY[i][n-1-i] =Truefor i in range(c, n):
isY[i][c] =True res = n*n
for y_val in range(3):
for non_y_val in range(3):
if y_val == non_y_val: continue changes =0for i in range(n):
for j in range(n):
if isY[i][j] and grid[i][j] != y_val:
changes +=1ifnot isY[i][j] and grid[i][j] != non_y_val:
changes +=1 res = min(res, changes)
return res
impl Solution {
pubfnminimum_operations(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let c = n/2;
letmut is_y =vec![vec![false;n];n];
for i in0..n { is_y[i][i]=true; is_y[i][n-1-i]=true; }
for i in c..n { is_y[i][c]=true; }
letmut res = (n*n) asi32;
for y_val in0..3 {
for non_y_val in0..3 {
if y_val==non_y_val { continue; }
letmut changes =0;
for i in0..n {
for j in0..n {
if is_y[i][j] && grid[i][j]!=y_val { changes+=1; }
if!is_y[i][j] && grid[i][j]!=non_y_val { changes+=1; }
}
}
res = res.min(changes);
}
}
res
}
}