Minimum Changes to Draw a Y
Problem statement
grid is an odd n by n matrix containing only 0, 1, and 2. The Y consists of both upper diagonals through the center and the vertical cells below the center.
Change the fewest cells so all Y cells share one value and all non-Y cells share a different value. Return the minimum changes.
Function
minimumYChanges(grid: int[][]) → intExamples
Example 1
grid = [[1,2,2],[1,1,0],[0,1,0]]return = 3Trying all distinct Y and background values yields a minimum of three changes.
Example 2
grid = [[0,0,0],[0,0,0],[0,0,0]]return = 4Use 0 for the five Y cells and recolor the four non-Y cells.
Constraints
3 <= n <= 501- n is odd.