Date: 2026-06-06
Time: 19:30
toeplitz-matrix/solution.pyThis file solves LeetCode 766 — Toeplitz Matrix. It determines whether a given matrix is a Toeplitz matrix, meaning every diagonal from top-left to bottom-right contains the same element. It's a single-responsibility module: one class, one method, one problem.
Solution.isToeplitzMatrix(matrix) -> bool — The only method. Takes an m×n matrix and returns True if every element equals the element diagonally above-left of it (matrix[i][j] == matrix[i-1][j-1]). Returns False on the first violation found.
The solution uses the pairwise neighbor comparison idiom rather than explicitly enumerating diagonals. Instead of iterating over each diagonal start point and walking down-right, it checks a local invariant at every interior cell: "does this cell match its diagonal predecessor?" This is equivalent because if every adjacent pair on a diagonal agrees, transitivity guarantees the entire diagonal is uniform.
The iteration starts at row 1 and column 1 (skipping the top row and left column), since those cells have no diagonal predecessor to compare against.
typing.List — used only for the type annotation.toeplitz-matrix/test_solution.py directly. The massive "Imported By" list in the prompt is an artifact of List being re-exported or resolved repo-wide — those test files import List from typing, not this module.1. Outer loop: rows i from 1 to m-1.
2. Inner loop: columns j from 1 to n-1.
3. Compare matrix[i][j] with matrix[i-1][j-1].
4. First mismatch → return False (short-circuit).
5. All cells pass → return True.
Total comparisons: at most (m-1) * (n-1). Time complexity is O(m·n), space is O(1).
True is returned, which is correct since every diagonal has exactly one element.None. The method trusts the caller to provide a well-formed rectangular matrix per the LeetCode contract. No bounds checking, no empty-matrix guard. This is appropriate — the problem guarantees valid input.