FastPrepMinimum Changes to Draw a Y

Minimum Changes to Draw a Y

ZipRecruiter logoZipRecruiter● MediumFULLTIMENEW GRADOA
Learn

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[][]) → int

Examples

Example 1

grid = [[1,2,2],[1,1,0],[0,1,0]]return = 3

Trying 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 = 4

Use 0 for the five Y cells and recolor the four non-Y cells.

Constraints

  • 3 <= n <= 501
  • n is odd.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int minimumYChanges(int[][] grid) {
    // Write your code here.
}
grid[[1,2,2],[1,1,0],[0,1,0]]
expected3
Checking account…