-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
49 lines (49 loc) · 1.23 KB
/
s1.cpp
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
// OJ: https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(1)
class Solution {
public:
int longestLine(vector<vector<int>>& mtx) {
if (mtx.empty() || mtx[0].empty()) return 0;
int M = mtx.size(), N = mtx[0].size();
int maxLen = 0;
for (int i = 0; i < M; ++i) {
int len = 0;
for (int j = 0; j < N; ++j) {
if (mtx[i][j] == 1) ++len;
else len = 0;
maxLen = max(maxLen, len);
}
}
for (int i = 0; i < N; ++i) {
int len = 0;
for (int j = 0; j < M; ++j) {
if (mtx[j][i] == 1) ++len;
else len = 0;
maxLen = max(maxLen, len);
}
}
for (int x = 0, y = N - 1; x < M; ) {
int len = 0;
for (int i = x, j = y; i < M && j < N; ++i, ++j) {
if (mtx[i][j] == 1) ++len;
else len = 0;
maxLen = max(maxLen, len);
}
if (y) --y;
else ++x;
}
for (int x = M - 1, y = N - 1; y >= 0; ) {
int len = 0;
for (int i = x, j = y; i < M && j >= 0; ++i, --j) {
if (mtx[i][j] == 1) ++len;
else len = 0;
maxLen = max(maxLen, len);
}
if (x) --x;
else --y;
}
return maxLen;
}
};