562. Longest Line of Consecutive One in Matrix
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the length of the longest line of consecutive 1s in a binary matrix. A "line" can be horizontal, vertical, diagonal (top-left to bottom-right), or anti-diagonal (top-right to bottom-left). We need to check all four directions for every possible line and find the maximum length among them.
Solution
Longest Line of Consecutive One in Matrix in Python
class Solution:
def longestLine(self, mat: List[List[int]]) -> int:
m = len(mat)
if m == 0:
return 0
n = len(mat[0])
if n == 0:
return 0
# dp[i][j][k] stores the length of the line of 1s ending at (i, j)
# k=0: horizontal, k=1: vertical, k=2: diagonal, k=3: anti-diagonal
dp = [[[0] * 4 for _ in range(n)] for _ in range(m)]
max_len = 0
for i in range(m):
for j in range(n):
if mat[i][j] == 1:
# Horizontal
dp[i][j][0] = dp[i][j-1][0] + 1 if j > 0 else 1
# Vertical
dp[i][j][1] = dp[i-1][j][1] + 1 if i > 0 else 1
# Diagonal
dp[i][j][2] = dp[i-1][j-1][2] + 1 if i > 0 and j > 0 else 1
# Anti-diagonal
dp[i][j][3] = dp[i-1][j+1][3] + 1 if i > 0 and j < n - 1 else 1
max_len = max(max_len, dp[i][j][0], dp[i][j][1], dp[i][j][2], dp[i][j][3])
return max_len
Longest Line of Consecutive One in Matrix in Java
class Solution {
public int longestLine(int[][] mat) {
int m = mat.length;
if (m == 0) return 0;
int n = mat[0].length;
if (n == 0) return 0;
// dp[i][j][k] stores the length of the line of 1s ending at (i, j)
// k=0: horizontal, k=1: vertical, k=2: diagonal, k=3: anti-diagonal
int[][][] dp = new int[m][n][4];
int maxLen = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 1) {
// Horizontal
dp[i][j][0] = (j > 0) ? dp[i][j - 1][0] + 1 : 1;
// Vertical
dp[i][j][1] = (i > 0) ? dp[i - 1][j][1] + 1 : 1;
// Diagonal (top-left to bottom-right)
dp[i][j][2] = (i > 0 && j > 0) ? dp[i - 1][j - 1][2] + 1 : 1;
// Anti-diagonal (top-right to bottom-left)
dp[i][j][3] = (i > 0 && j < n - 1) ? dp[i - 1][j + 1][3] + 1 : 1;
maxLen = Math.max(maxLen, dp[i][j][0]);
maxLen = Math.max(maxLen, dp[i][j][1]);
maxLen = Math.max(maxLen, dp[i][j][2]);
maxLen = Math.max(maxLen, dp[i][j][3]);
}
}
}
return maxLen;
}
}
Longest Line of Consecutive One in Matrix in C++
class Solution {
public:
int longestLine(vector<vector<int>>& mat) {
int m = mat.size();
if (m == 0) return 0;
int n = mat[0].size();
if (n == 0) return 0;
// dp[i][j][k] stores the length of the line of 1s ending at (i, j)
// k=0: horizontal, k=1: vertical, k=2: diagonal, k=3: anti-diagonal
vector<vector<vector<int>>> dp(m, vector<vector<int>>(n, vector<int>(4, 0)));
int maxLen = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 1) {
// Horizontal
dp[i][j][0] = (j > 0) ? dp[i][j - 1][0] + 1 : 1;
// Vertical
dp[i][j][1] = (i > 0) ? dp[i - 1][j][1] + 1 : 1;
// Diagonal (top-left to bottom-right)
dp[i][j][2] = (i > 0 && j > 0) ? dp[i - 1][j - 1][2] + 1 : 1;
// Anti-diagonal (top-right to bottom-left)
dp[i][j][3] = (i > 0 && j < n - 1) ? dp[i - 1][j + 1][3] + 1 : 1;
maxLen = max({maxLen, dp[i][j][0], dp[i][j][1], dp[i][j][2], dp[i][j][3]});
}
}
}
return maxLen;
}
};
Longest Line of Consecutive One in Matrix in TypeScript
function longestLine(mat: number[][]): number {
const m = mat.length;
if (m === 0) return 0;
const n = mat[0].length;
if (n === 0) return 0;
// dp[i][j][k] stores the length of the line of 1s ending at (i, j)
// k=0: horizontal, k=1: vertical, k=2: diagonal, k=3: anti-diagonal
const dp: number[][][] = Array.from({ length: m }, () =>
Array.from({ length: n }, () => Array(4).fill(0))
);
let maxLen = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (mat[i][j] === 1) {
// Horizontal
dp[i][j][0] = (j > 0) ? dp[i][j - 1][0] + 1 : 1;
// Vertical
dp[i][j][1] = (i > 0) ? dp[i - 1][j][1] + 1 : 1;
// Diagonal (top-left to bottom-right)
dp[i][j][2] = (i > 0 && j > 0) ? dp[i - 1][j - 1][2] + 1 : 1;
// Anti-diagonal (top-right to bottom-left)
dp[i][j][3] = (i > 0 && j < n - 1) ? dp[i - 1][j + 1][3] + 1 : 1;
maxLen = Math.max(maxLen, dp[i][j][0], dp[i][j][1], dp[i][j][2], dp[i][j][3]);
}
}
}
return maxLen;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.