Problem · Matrix

Write 'Y' on Matrix

Learn this problem
MediumDatabricks logoDatabricksINTERNOA

Problem statement

You are given an odd n × n matrix containing only 0, 1, and 2.

The cells of the letter Y are both diagonals from the top corners through the center, followed by the center column from the center to the bottom. Choose one digit for all Y cells and a different digit for every non-Y cell.

Return the minimum number of cells that must change over all six ordered choices of distinct Y and background digits.

Function

writeLMatrix(matrix: int[][]) → int

Examples

Example 1

matrix = [[1, 0, 2], [1, 2, 0], [0, 2, 0]]return = 2

Using 2 for the Y and 0 for the background requires changing two cells.

Example 2

matrix = [[2, 0, 0, 0, 2], [1, 2, 1, 2, 0], [0, 1, 2, 1, 0], [0, 0, 2, 1, 1], [1, 1, 2, 1, 1]]return = 8

Using 2 for the Y and 1 for all remaining cells requires changing the eight zeroes.

Constraints

  • 5 ≤ matrix.length ≤ 99
  • 0 ≤ matrix[i][j] ≤ 2

More Databricks problems

drafts saved locally
public int writeLMatrix(int[][] matrix) {
  // write your code here
}
matrix[[1, 0, 2], [1, 2, 0], [0, 2, 0]]
expected2
checking account