Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
Example 1:
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] Output: true Explanation: In the above grid, the diagonals are: "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]". In each diagonal all elements are the same, so the answer is True.
Example 2:
Input: matrix = [[1,2],[2,2]] Output: false Explanation: The diagonal "[1, 2]" has different elements.
Constraints:
m == matrix.lengthn == matrix[i].length1 <= m, n <= 200 <= matrix[i][j] <= 99Follow up:
matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?matrix is so large that you can only load up a partial row into the memory at once?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks us to check if a given grid of numbers is a special type of matrix called a Toeplitz matrix. To do this the brute force method will compare every number to a specific other number in the grid.
Here's how the algorithm would work step-by-step:
def is_toeplitz_matrix(matrix):
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
for column_start_index in range(number_of_columns):
# Check diagonals starting from the top row
value_to_compare = matrix[0][column_start_index]
row_index = 1
column_index = column_start_index + 1
while row_index < number_of_rows and column_index < number_of_columns:
if matrix[row_index][column_index] != value_to_compare:
return False
row_index += 1
column_index += 1
# Skip the top-left since those diagonals were already tested
for row_start_index in range(1, number_of_rows):
value_to_compare = matrix[row_start_index][0]
row_index = row_start_index + 1
column_index = 1
# Check diagonals starting from the first column
while row_index < number_of_rows and column_index < number_of_columns:
if matrix[row_index][column_index] != value_to_compare:
return False
row_index += 1
column_index += 1
return TrueThe key is to realize that in a Toeplitz matrix, all diagonals have the same value. So, to check if a matrix is Toeplitz, we only need to compare each element to the element diagonally above and to the left of it.
Here's how the algorithm would work step-by-step:
def is_toeplitz_matrix(matrix):
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
# Start from the second row and second column.
for row_index in range(1, number_of_rows):
for column_index in range(1, number_of_columns):
# Compare with the element diagonally above and to the left.
if matrix[row_index][column_index] != matrix[row_index - 1][column_index - 1]:
# If elements do not match, it's not a Toeplitz matrix.
return False
# If all elements match, it's a Toeplitz matrix.
return True| Case | How to Handle |
|---|---|
| Null or empty matrix | Return true immediately as an empty matrix trivially satisfies the Toeplitz property. |
| 1x1 matrix | Return true, as a single element matrix trivially satisfies the condition. |
| Matrix with only one row or one column | Return true because only one diagonal exists and all elements on it are equal. |
| Large matrix (performance considerations) | Ensure the solution iterates efficiently to avoid exceeding time limits for large inputs, and potentially consider using generators to reduce memory footprint. |
| Matrix with all elements the same | The algorithm will correctly identify this as a Toeplitz matrix. |
| Integer overflow if elements are very large | The solution should not involve calculations that could lead to an integer overflow; comparison should be safe. |
| Matrix with negative numbers, zeros, and positive numbers | The algorithm should handle all number types correctly through direct comparison. |
| Non-square Toeplitz matrices | The algorithm should correctly verify if the matrix is Toeplitz even if the number of rows and columns are different. |