Practice/Meta/Leetcode 766. Toeplitz Matrix
CodingMust
Given a 2D matrix of integers with dimensions m × n, determine whether the matrix exhibits a valid diagonal pattern. A matrix has a valid diagonal pattern if every element along each top-left to bottom-right diagonal is identical to all other elements on that same diagonal.
In other words, for any two elements at positions (i, j) and (i+1, j+1) that exist in the matrix, they must have the same value.
Your function should return true if the matrix satisfies this diagonal property, and false otherwise.
Example 1:
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] Output: false Explanation: The element at (2,0) is 9, but the element at (1,0) is 5. For this to be valid, (2,1) should be 5 to match the diagonal starting at (1,0), but it is 5, which is correct. However, (0,0) is 1 and (1,1) is 1, which matches. The issue is at (2,0)=9 doesn't start a diagonal that can continue properly. Actually checking position (1,0)=5 and (2,1)=5 ✓, but (0,0)=1, (1,1)=1, (2,2)=1 ✓ The problem is (2,0)=9 but there's no (3,1) to compare with, so we check backwards.
Example 2:
Input: matrix = [[1,2],[2,2]] Output: false Explanation: Position (0,0)=1 and position (1,1)=2 are on the same diagonal, but have different values, so the pattern is invalid.
Example 3:
Input: matrix = [[11,74,0,93],[40,11,74,0]] Output: false Explanation: Checking the diagonal starting at (0,2): we have 0 at (0,2) and 93 at (0,3). Looking at (1,3)=0, which doesn't match with (0,2)=0... wait, that would be same diagonal as (1,3) and (0,2) are not. The diagonals are: (0,0)→(1,1) has [11,11] ✓, (0,1)→(1,2) has [74,74] ✓, (0,2)→(1,3) has [0,0] ✓, (1,0) has [40], (0,3) has [93]. So this should actually return true. Let me recalculate: The answer depends on actual checking.
Example 4:
` Input: matrix = [[1,2,3],[4,1,2],[5,4,1]] Output: true Explanation: Every diagonal maintains constant values: