Skip to content

Commit 53fc10e

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (38.30%), Memory - 17.7 MB (11.70%)
1 parent dc11579 commit 53fc10e

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

0777-toeplitz-matrix/solution.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
# Approach 2: Compare with Top-Left Neighbor
1+
# Approach 2: Compare With Top-Left Neighbor
22

3-
# Time: O(M * N)
3+
# m = no. of rows, n = no. of cols
4+
# Time: O(m * n)
45
# Space: O(1)
56

6-
# Every element belongs to some diagonal, and it's previous element (if it exists) is it's top-left neighbor. Thus, for the square (r, c), we only need to check r == 0 OR c == 0 OR matrix[r-1][c-1] == matrix[r][c]
7-
87
class Solution:
98
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
10-
return all(r == 0 or c == 0 or matrix[r-1][c-1] == val
11-
for r, row in enumerate(matrix)
12-
for c, val in enumerate(row))
13-
9+
return all(r == 0 or c == 0 or matrix[r - 1][c - 1] == val
10+
for r, row in enumerate(matrix)
11+
for c, val in enumerate(row))
12+

0 commit comments

Comments
 (0)