Given an m x n binary matrix mat, return the length of the longest line of consecutive one in the matrix.
The line could be horizontal, vertical, diagonal, or anti-diagonal.
Example 1:
Input: mat = [[0,1,1,0],[0,1,1,0],[0,0,0,1]] Output: 3
Example 2:
Input: mat = [[1,1,1,1],[0,1,1,0],[0,0,0,1]] Output: 4
Constraints:
m == mat.lengthn == mat[i].length1 <= m, n <= 1041 <= m * n <= 104mat[i][j] is either 0 or 1.Problem Overview: Given a binary matrix, find the maximum number of consecutive 1s that appear in a straight line. A valid line can be horizontal, vertical, diagonal, or anti-diagonal. The goal is to scan the grid and return the length of the longest such sequence.
Approach 1: Brute Force Directional Scan (O(m*n*max(m,n)) time, O(1) space)
The straightforward method checks every cell containing 1 and extends in four directions: right, down, diagonal, and anti-diagonal. For each direction, keep moving while the next cell remains 1. Track the length and update the global maximum. This approach is simple but inefficient because the same sequences are recomputed multiple times. In dense matrices, repeated directional scans significantly increase runtime.
Approach 2: Dynamic Programming with Direction States (O(m*n) time, O(m*n) space)
The efficient solution uses dynamic programming to track streak lengths ending at each cell. For every matrix[i][j] == 1, maintain four DP values: horizontal, vertical, diagonal, and anti-diagonal. Each value extends from a previously computed neighbor. Horizontal depends on (i, j-1), vertical on (i-1, j), diagonal on (i-1, j-1), and anti-diagonal on (i-1, j+1). Add 1 to the corresponding previous value and store it. This avoids re-scanning sequences and ensures each cell is processed once.
Implementation typically uses a 3D DP array dp[i][j][4] or four separate matrices. When the cell value is 0, all streak counts remain zero. When it's 1, compute the four directions using previously calculated states. Track the maximum value seen across all directions. The algorithm runs in O(m*n) time because each cell performs constant work.
An alternative optimization compresses memory by keeping only the previous row for vertical and diagonal transitions while computing the current row for horizontal and anti-diagonal values. This reduces space to O(n) while maintaining the same time complexity.
This problem is a classic application of array traversal combined with directional dynamic programming. The key insight is recognizing overlapping subproblems: the longest line ending at a cell depends on previously solved neighbors rather than scanning entire lines repeatedly.
Recommended for interviews: The dynamic programming approach is what interviewers expect. Brute force shows you understand the four valid directions, but the DP solution demonstrates optimization skills and the ability to reuse computed state. Explaining how each direction depends on a specific neighbor is usually the critical insight interviewers look for.
We define f[i][j][k] to represent the length of the longest consecutive 1s ending at (i, j) in direction k. The value range of k is 0, 1, 2, 3, representing horizontal, vertical, diagonal, and anti-diagonal directions, respectively.
We can also use four 2D arrays to represent the length of the longest consecutive
1s in the four directions.
We traverse the matrix, and when we encounter 1, we update the value of f[i][j][k]. For each position (i, j), we only need to update the values in its four directions. Then we update the answer.
The time complexity is O(m times n), and the space complexity is O(m times n), where m and n are the number of rows and columns in the matrix, respectively.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Directional Scan | O(m*n*max(m,n)) | O(1) | Good for understanding the problem and verifying all four directions before optimizing |
| Dynamic Programming with 4 Direction States | O(m*n) | O(m*n) | Standard optimal solution for interviews and large matrices |
| Space Optimized DP | O(m*n) | O(n) | Useful when memory usage matters while keeping the same optimal runtime |
LeetCode 562. Longest Line of Consecutive One in Matrix • Happy Coding • 2,563 views views
Watch 9 more video solutions →Practice Longest Line of Consecutive One in Matrix with our built-in code editor and test cases.
Practice on FleetCode