forked from Kyrylo-Ktl/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToeplitz Matrix.py
More file actions
30 lines (23 loc) · 860 Bytes
/
Toeplitz Matrix.py
File metadata and controls
30 lines (23 loc) · 860 Bytes
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
from typing import Generator, List
class Solution:
"""
Time: O(n*m)
Memory: O(n+m)
"""
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
return all(len(set(diag)) == 1 for diag in self._diagonals_generator(matrix))
@staticmethod
def _diagonals_generator(matrix: List[List[int]]) -> Generator:
n, m = len(matrix), len(matrix[0])
for i in range(n):
yield [matrix[i + k][k] for k in range(min(m, n - i))]
for j in range(1, m):
yield [matrix[k][j + k] for k in range(min(m - j, n))]
class Solution:
"""
Time: O(n*m)
Memory: O(1)
"""
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
n, m = len(matrix), len(matrix[0])
return all(matrix[i][j] == matrix[i + 1][j + 1] for i in range(n - 1) for j in range(m - 1))