-
Notifications
You must be signed in to change notification settings - Fork 0
/
562_Longest_Line_of_Consecutive_One_in_Matrix.py
40 lines (33 loc) · 1.34 KB
/
562_Longest_Line_of_Consecutive_One_in_Matrix.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
class Solution:
def longestLine(self, M: List[List[int]]) -> int:
if not M:
return 0
record = [[[0 for _ in range(4)] for _ in range(len(M[0]))] for _ in range(len(M))]
result = 0
if M[0][0]:
result = 1
for i in range(4):
record[0][0][i] = 1
for i in range(1, len(M)):
if M[i][0]:
record[i][0][0] = 1
record[i][0][1] = 1
record[i][0][2] = record[i - 1][0][2] + 1
record[i][0][3] = 1
result = max(result, record[i][0][2])
for j in range(1, len(M[0])):
for i in range(len(M)):
if M[i][j]:
record[i][j][0] = record[i][j - 1][0] + 1
if i == 0:
record[i][j][1] = 1
record[i][j][2] = 1
else:
record[i][j][1] = record[i - 1][j - 1][1] + 1
record[i][j][2] = record[i - 1][j][2] + 1
if i == len(M) - 1:
record[i][j][3] = 1
else:
record[i][j][3] = record[i + 1][j - 1][3] + 1
result = max(result, record[i][j][0], record[i][j][1], record[i][j][2], record[i][j][3])
return result