562.Longest-Line-of-Consecutive-One-in-Matrix
562. Longest Line of Consecutive One in Matrix
题目地址
https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix/
题目描述
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.
代码
Approach #1 Brute Force
Time: O(N^2) && Space: O(1)
class Solution {
public int longestLine(int[][] M) {
if (M.length == 0) return 0;
int ones = 0;
// horizontal
for (int i = 0; i < M.length; i++) {
int count = 0;
for (int j = 0; j < M[0].length; j++) {
if (M[i][j] == 1) {
count++;
ones = Math.max(ones, count);
} else {
count = 0;
}
}
}
// vertical
for (int i = 0; i < M[0].length; i++) {
int count = 0;
for (int j = 0; j < M.length; j++) {
if (M[j][i] == 1) {
count++;
ones = Math.max(ones, count);
} else {
count = 0;
}
}
}
// upper diagonal
for (int i = 0; i < M[0].length || i < M.length; i++) {
int count = 0;
for (int x = 0; y = i; x < M.length && y < M[0].length; x++, y++) {
if (M[x][y] == 1) {
count++;
ones = Math.max(ones, count);
} else {
count = 0;
}
}
}
// lower diagonal
for (int i = 0; i < M[0].length || i < M.length; i++) {
int count = 0;
for (int x = i, y = 0; x < M.length && y < M[0].length; x++, y++) {
if (M[x][y] == 1) {
count++;
ones = Math.max(ones, count);
} else {
count = 0;
}
}
}
// upper anti-diagonal
for (int i = 0; i < M[0].length || i < M.length; i++) {
int count = 0;
for (int x = 0, y = M[0].length - i - 1; x < M.length && y >= 0; x++, y--) {
if (M[x][y] == 1) {
count++;
ones = Math.max(ones, count);
} else {
count = 0;
}
}
}
// lower anti-diagonal
for (int i = 0; i < M[0].length || i < M.length; i++) {
int count = 0;
for (int x = i, y = M[0].length - 1; x < M.length && y >= 0; x++, y--) {
if (M[x][y] == 1) {
count++;
ones = Math.max(ones, count);
} else {
count = 0;
}
}
}
return ones;
}
}
Approach #3 3D Dynamic Programming
Time complexity && Space complexity : O(mn)
class Solution {
public int longestLine(int[][] M) {
if (M.length == 0) return 0;
int ones = 0;
int[][][] dp = new int[M.length][M[0].length][4];
for (int i = 0; i < M.length; i++) {
for (int j = 0; j < M[0].length; j++) {
if (M[i][j] == 1) {
dp[i][j][0] = j > 0 ? dp[i][j - 1][0] + 1 : 1;
dp[i][j][1] = i > 0 ? dp[i - 1][j][1] + 1: 1;
dp[i][j][2] = (i > 0 && j > 0) ? dp[i - 1][j - 1][2] + 1 : 1;
dp[i][j][3] = (i > 0 && j < M[0].length - 1) ? dp[i - 1][j + 1][3] + 1 : 1;
ones = Math.max(ones, Math.max(
Math.max(dp[i][j][0], dp[i][j][1]),
Math.max(dp[i][j][2], dp[i][j][3])
));
}
}
}
return ones;
}
}
Approach #3 2D Dynamic Programming
class Solution {
public int longestLine(int[][] M) {
if (M.length == 0) return 0;
int ones = 0;
int[][] dp = new int[M[0].length][4];
for (int i = 0; i < M.length; i++) {
int old = 0;
for (int j = 0; j < M[0].length; j++) {
if (M[i][j] == 1) {
dp[j][0] = j > 0 ? dp[j - 1][0] + 1 : 1; // horizontal
dp[j][1] = i > 0 ? dp[j][1] + 1 : 1; // vertial
int prev = dp[j][2];
dp[j][2] = (i > 0 && j > 0) ? old + 1 : 1;
old = prev;
dp[j][3] = (i > 0 && j < M[0].length - 1) ? dp[j + 1][3] + 1 : 1;
ones = Math.max(ones,
Math.max(Math.max(dp[j][0], dp[j][1]),
Math.max(dp[j][2], dp[j][3])));
} else {
old = dp[j][2];
dp[j][0] = dp[j][1] = dp[j][2] = dp[j][3] = 0;
}
}
}
return ones;
}
}
Last updated