-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestLine.java
93 lines (84 loc) · 2.03 KB
/
LongestLine.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horizontal, vertical, diagonal or anti-diagonal.
Example:
Input:
[[0,1,1,0],
[0,1,1,0],
[0,0,0,1]]
Output: 3
Hint: The number of elements in the given matrix will not exceed 10,000.
*/
class Solution {
public:
int longestLine(vector<vector<int>>& M) {
int max1=0;
int i,j,m=M.size();
if(!m) return 0;
int n=M[0].size();
for(i=0;i<m;i++){
int b=0;
for(j=0;j<n;j++)
if(M[i][j]==1) {
b++;
max1=max(max1,b);
}
else b=0;
}
for(i=0;i<n;i++){
int b=0;
for(j=0;j<m;j++)
if(M[j][i]==1) {
b++;
max1=max(max1,b);
}
else b=0;
}
//Diagonal
for(i=0;i<m;i++){
int b=0;j=0;int x=i;
while(x<m&&j<n){
if(M[x][j]==1) {
b++;
max1=max(max1,b);
}
else b=0;
x++;j++;
}
}
for(j=1;j<n;j++){
int b=0;i=0;int y=j;
while(i<m&&y<n){
if(M[i][y]==1) {
b++;
max1=max(max1,b);
}
else b=0;
i++;y++;
}
}
// Anti diagonal
for(j=0;j<n;j++){
int b=0;i=0;int y=j;
while(i>=0&&i<m&&y>=0&&y<n){
if(M[i][y]==1) {
b++;
max1=max(max1,b);
}
else b=0;
y--;i++;
}
}
for(i=1;i<m;i++){
int b=0;j=n-1;int x=i;
while(x<m&&j>=0){
if(M[x][j]==1) {
b++;
max1=max(max1,b);
}
else b=0;
j--;x++;
}
}
return max1;
}
};