Minimum Operations to Write the Letter Y on a Grid
You are given a 0-indexed n 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 the minimum 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 to 0, 1, or 2.
Example 1
1 2 2 1 1 0 0 1 0
Input
grid = [[1,2,2],[1,1,0],[0,1,0]]Output
3After 3 operations, all cells belonging to Y can have value 1 while all other cells have value 0, and this is minimum.
Example 2
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
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
12After 12 operations, all cells belonging to Y can have value 0 while all other cells have value 2, and this is minimum.
Constraints
- 3 <= n <= 49
- n == grid.length == grid[i].length
- 0 <= grid[i][j] <= 2
- n is odd.