-
Notifications
You must be signed in to change notification settings - Fork 46
/
_562.java
30 lines (28 loc) · 968 Bytes
/
_562.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
/**
* LeetCode 562 - Longest Line of Consecutive One in Matrix
* <p>
* Brute-force
* O(RC) runtime
*/
public class _562 {
private final int[] DX = {0, 1, 1, 1};
private final int[] DY = {1, 0, 1, -1};
private boolean isOne(int[][] M, int i, int j) {
return i >= 0 && i < M.length && j >= 0 && j < M[i].length && M[i][j] == 1;
}
public int longestLine(int[][] M) {
if (M.length == 0 || M[0].length == 0) return 0;
int ans = 0;
for (int i = 0; i < M.length; i++)
for (int j = 0; j < M[i].length; j++) {
for (int k = 0; k < DX.length; k++) {
if (isOne(M, i, j) && !isOne(M, i - DX[k], j - DY[k])) {
int cnt = 0;
for (int x = i, y = j; isOne(M, x, y); cnt++, x += DX[k], y += DY[k]) ;
ans = Math.max(ans, cnt);
}
}
}
return ans;
}
}