DevPath

← All problems

Toeplitz Matrix

Easy matrix

Description

Return true if every diagonal from top-left to bottom-right has the same value.

Constraints

1 <= rows, cols <= 1000

Examples

Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]

Output: true

Hints (1)
  • Compare each cell with the one above and to the left.

Solutions

Compare with the up-left neighbour. · Time O(rows*cols) · Space O(1)

func isToeplitzMatrix(matrix [][]int) bool {
    for r := 1; r < len(matrix); r++ {
        for c := 1; c < len(matrix[0]); c++ {
            if matrix[r][c] != matrix[r-1][c-1] {
                return false
            }
        }
    }
    return true
}

Create a free account to submit solutions, track progress and save problems.

Create account