Skip to content

Commit d558301

Browse files
author
Partho Biswas
committed
562. Longest Line of Consecutive One in Matrix
1 parent 3b4e17f commit d558301

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
620620
|43| **[64. Minimum Path Sum](https://tinyurl.com/ugnoql2)** | [Python](https://tinyurl.com/wu6rdaw/64_Minimum_Path_Sum.py), [Swift](https://tinyurl.com/wuja3c4/64_Minimum_Path_Sum.swift)| --- | Medium | --- |
621621
|44| **[91. Decode Ways](https://tinyurl.com/t5yx86t)** | [Python](https://tinyurl.com/wu6rdaw/91_Decode_Ways.py), [Swift](https://tinyurl.com/wuja3c4/91_Decode_Ways.swift)| [Vid 1](https://tinyurl.com/wxc7jlj) | Medium | --- |
622622
|45| **[975. Odd Even Jump](https://tinyurl.com/y5ovyklj)** | [Python](https://tinyurl.com/wu6rdaw/975_Odd_Even_Jump.py), [Swift](https://tinyurl.com/wuja3c4/975_Odd_Even_Jump.swift)| [Vid 1](https://tinyurl.com/vxyed3g), **[Art 1](https://tinyurl.com/wbjrnyt)** | Hard | DP using stack. Vary tricky and Interesting problem, loved it |
623+
|46| **[562. Longest Line of Consecutive One in Matrix](https://tinyurl.com/u2fb4a6)** | [Python](https://tinyurl.com/wu6rdaw/562_Longest_Line_of_Consecutive_One_in_Matrix.py.py), [Swift](https://tinyurl.com/wuja3c4/562_Longest_Line_of_Consecutive_One_in_Matrix.py.swift)| [Art 1](https://tinyurl.com/vqppbae), [Art 2](https://tinyurl.com/t3xtfq8) | Medium | |
623624

624625

625626
</p>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Approach 1: Brute force. Time limit exceeded. 57 / 57 test cases passed, but took too long.
2+
# # https://tinyurl.com/vqppbae
3+
class Solution(object):
4+
def longestLine(self, M):
5+
"""
6+
:type M: List[List[int]]
7+
:rtype: int
8+
"""
9+
rowCount, colCount = len(M), len(M[0]) if M else 0
10+
if rowCount <= 0 or colCount <= 0:
11+
return 0
12+
maxOnes = 0
13+
14+
# Checks horizontal lines for max ones
15+
for rowIdx in range(rowCount):
16+
currentOnes = 0
17+
for colIdx in range(colCount):
18+
if M[rowIdx][colIdx] == 1:
19+
currentOnes += 1
20+
maxOnes = max(maxOnes, currentOnes)
21+
else:
22+
currentOnes = 0
23+
24+
# Checks vertical lines for max ones
25+
for colIdx in range(colCount):
26+
currentOnes = 0
27+
for rowIdx in range(rowCount):
28+
if M[rowIdx][colIdx] == 1:
29+
currentOnes += 1
30+
maxOnes = max(maxOnes, currentOnes)
31+
else:
32+
currentOnes = 0
33+
34+
# Checks upper diagonal lines for max ones
35+
for diagonalCol in range(max(rowCount, colCount)):
36+
currentOnes = 0
37+
for rowIdx, colIdx in zip(range(rowCount), range(diagonalCol, colCount)):
38+
if M[rowIdx][colIdx] == 1:
39+
currentOnes += 1
40+
maxOnes = max(maxOnes, currentOnes)
41+
else:
42+
currentOnes = 0
43+
44+
# Checks lower diagonal lines for max ones
45+
for diagonalCol in range(max(rowCount, colCount)):
46+
currentOnes = 0
47+
for rowIdx, colIdx in zip(range(diagonalCol, rowCount), range(colCount)):
48+
if M[rowIdx][colIdx] == 1:
49+
currentOnes += 1
50+
maxOnes = max(maxOnes, currentOnes)
51+
else:
52+
currentOnes = 0
53+
54+
# Checks upper anti-diagonal lines for max ones
55+
for diagonalCol in range(max(rowCount, colCount)):
56+
currentOnes = 0
57+
for rowIdx, colIdx in zip(range(rowCount), range(colCount - 1 - diagonalCol, -1, -1)):
58+
if M[rowIdx][colIdx] == 1:
59+
currentOnes += 1
60+
maxOnes = max(maxOnes, currentOnes)
61+
else:
62+
currentOnes = 0
63+
64+
# Checks lower anti-diagonal lines for max ones
65+
for diagonalCol in range(max(rowCount, colCount)):
66+
currentOnes = 0
67+
for rowIdx, colIdx in zip(range(diagonalCol, rowCount), range(colCount - 1, -1, -1)):
68+
if M[rowIdx][colIdx] == 1:
69+
currentOnes += 1
70+
maxOnes = max(maxOnes, currentOnes)
71+
else:
72+
currentOnes = 0
73+
74+
return maxOnes
75+
76+
77+
78+
79+
# Approach 2: Better Brute force. Accepted
80+
# https://tinyurl.com/t3xtfq8
81+
from collections import defaultdict
82+
83+
class Solution(object):
84+
def longestLine(self, M):
85+
rowCount, colCount = len(M), len(M[0]) if M else 0
86+
if rowCount <= 0 or colCount <= 0:
87+
return 0
88+
89+
maxOnes = 0
90+
linesDict = defaultdict(list)
91+
for rowIdx in range(rowCount):
92+
for colIdx in range(colCount):
93+
val = M[rowIdx][colIdx]
94+
linesDict[0, rowIdx].extend([val])
95+
linesDict[1, colIdx].extend([val])
96+
linesDict[2, rowIdx + colIdx].extend([val])
97+
linesDict[3, rowIdx - colIdx].extend([val])
98+
99+
for line in linesDict.values():
100+
currentMaxOnes = self.onesCount(line)
101+
maxOnes = max(maxOnes, currentMaxOnes)
102+
return maxOnes
103+
104+
def onesCount(self, line):
105+
ans, count = 0, 0
106+
for num in line:
107+
if num == 1:
108+
count += 1
109+
ans = max(ans, count)
110+
else:
111+
count = 0
112+
return ans
113+
114+
115+
# Approach 3: 3-D Dynamic Programming
116+
# https://tinyurl.com/vqppbae
117+
from collections import defaultdict
118+
119+
class Solution(object):
120+
def longestLine(self, M):
121+
rowCount, colCount = len(M), len(M[0]) if M else 0
122+
if rowCount <= 0 or colCount <= 0:
123+
return 0
124+
125+
maxOnes = 0
126+
dp = [[[0 for _ in range(4)] for _ in range(colCount)] for _ in range(rowCount)]
127+
for rowIdx in range(rowCount):
128+
for colIdx in range(colCount):
129+
val = M[rowIdx][colIdx]
130+
if val:
131+
# 0 = Horizontal, 1 = Vertical, 2 = Diagonal and 3 = Anti-diagonal
132+
dp[rowIdx][colIdx][0] = (dp[rowIdx][colIdx - 1][0] + 1) if colIdx > 0 else 1
133+
dp[rowIdx][colIdx][1] = (dp[rowIdx - 1][colIdx][1] + 1) if rowIdx > 0 else 1
134+
dp[rowIdx][colIdx][2] = (dp[rowIdx - 1][colIdx - 1][2] + 1) if (colIdx > 0 and rowIdx > 0) else 1
135+
dp[rowIdx][colIdx][3] = (dp[rowIdx - 1][colIdx + 1][3] + 1) if (colIdx < colCount - 1 and rowIdx > 0) else 1
136+
maxOnes = max([maxOnes, dp[rowIdx][colIdx][0], dp[rowIdx][colIdx][1], dp[rowIdx][colIdx][2], dp[rowIdx][colIdx][3]])
137+
138+
return maxOnes

0 commit comments

Comments
 (0)