-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path315.py
66 lines (55 loc) · 1.45 KB
/
315.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Problem:
In linear algebra, a Toeplitz matrix is one in which the elements on any given diagonal
from top left to bottom right are identical.
Here is an example:
1 2 3 4 8
5 1 2 3 4
4 5 1 2 3
7 4 5 1 2
Write a program to determine whether a given input is a Toeplitz matrix.
"""
from typing import List
def is_toeplitz_matrix(matrix: List[List[int]]) -> bool:
n = len(matrix)
m = len(matrix[0])
# checking the diagonals starting from the left edge
for i in range(n):
val = matrix[i][0]
for row, col in zip(range(i, n), range(m)):
if matrix[row][col] != val:
return False
# checking the diagonals starting from the top edge
for i in range(1, m):
val = matrix[0][i]
for row, col in zip(range(n), range(i, m)):
if matrix[row][col] != val:
return False
return True
if __name__ == "__main__":
print(
is_toeplitz_matrix(
[
[1, 2, 3, 4, 8],
[5, 1, 2, 3, 4],
[4, 5, 1, 2, 3],
[7, 4, 5, 1, 2]
]
)
)
print(
is_toeplitz_matrix(
[
[1, 2, 3, 4, 8],
[5, 1, 2, 3, 4],
[4, 5, 1, 2, 3],
[7, 4, 5, 1, 1]
]
)
)
"""
SPECS:
TIME COMPLEXITY: O(n x m)
SPACE COMPLEXITY: O(1) [as zip and range both are generator functions]
[n = rows, m = columns]
"""