LeetCode 562: Longest Line of Consecutive One in Matrix Solution

Master LeetCode problem 562 (Longest Line of Consecutive One in Matrix), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

562. Longest Line of Consecutive One in Matrix

Problem Explanation

Explanation:

This problem asks us to find the longest line of consecutive 1s in a matrix. The matrix can have 4 directions where the consecutive 1s can be counted: horizontal, vertical, diagonal, and anti-diagonal.

We can solve this problem efficiently by iterating through the matrix and keeping track of the lengths of consecutive 1s in each direction. We can use dynamic programming to update the lengths based on the current cell's value and the neighboring cells.

Here are the steps for the algorithm:

  1. Initialize variables for the maximum length of consecutive 1s in each direction.
  2. Iterate through the matrix and update the lengths of consecutive 1s for each direction.
  3. Return the maximum length among all directions.

Time complexity: O(m*n) where m is the number of rows and n is the number of columns in the matrix. Space complexity: O(n) where n is the number of columns in the matrix.

:

Solution Code

class Solution {
    public int longestLine(int[][] mat) {
        int m = mat.length;
        if (m == 0) return 0;
        int n = mat[0].length;
        
        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] == 0) continue;
                
                for (int k = 0; k < 4; k++) {
                    dp[i][j][k] = 1;
                }
                
                if (j > 0) dp[i][j][0] += dp[i][j-1][0]; // horizontal
                if (i > 0) dp[i][j][1] += dp[i-1][j][1]; // vertical
                if (i > 0 && j > 0) dp[i][j][2] += dp[i-1][j-1][2]; // diagonal
                if (i > 0 && j < n-1) dp[i][j][3] += dp[i-1][j+1][3]; // anti-diagonal
                
                maxLen = Math.max(maxLen, Math.max(dp[i][j][0], Math.max(dp[i][j][1], Math.max(dp[i][j][2], dp[i][j][3]))));
            }
        }
        
        return maxLen;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 562 (Longest Line of Consecutive One in Matrix)?

This page provides optimized solutions for LeetCode problem 562 (Longest Line of Consecutive One in Matrix) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 562 (Longest Line of Consecutive One in Matrix)?

The time complexity for LeetCode 562 (Longest Line of Consecutive One in Matrix) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 562 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 562 in Java, C++, or Python.

Back to LeetCode Solutions