622. Longest Line of Consecutive One in Matrix

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

Explanation:

Horizontal lines: Maximum consecutive ones are 2 (rows 1 and 2).

Vertical lines: Maximum consecutive ones are 2 (columns 2 and 3).

Diagonal lines: Maximum consecutive ones are 3 (from top-left to bottom-right).

Example 2:

Input: mat = [[1,1,1,1],[0,1,1,0],[0,0,0,1]]

Output: 4

Explanation:

Horizontal lines: Maximum consecutive ones are 4 (row 1).

Vertical lines: Maximum consecutive ones are 2 (column 2).

Diagonal lines: Maximum consecutive ones are 3 (top-left to bottom-right).

Now Your Turn!

Pick the correct output for the given input

Input: mat = [[1,1,0,1],[0,0,1,1],[1,0,0,1]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 104
  • 1 <= m * n <= 104
  • mat[i][j] is either 0 or 1.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int longestLine(vector<vector<int>>& mat) {
// Your code goes here
}
};
Test Case

Input:

Mat