bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 562. Longest Line of Consecutive One in Matrix

562. Longest Line of Consecutive One in Matrix

Leetcode Solutions

UsingD Dynamic Programming

  1. Initialize four 1D arrays: horizontal, vertical, diagonal, antiDiagonal each of size n (number of columns).
  2. Initialize maxLen to 0 to keep track of the maximum length of consecutive ones.
  3. Iterate through each cell of the matrix row by row and column by column.
  4. Update the DP arrays based on the current cell value:
    • If the cell contains a 1, update the arrays by incrementing the corresponding counts from the previous state.
    • If the cell contains a 0, reset the counts in the arrays to 0.
  5. After updating the DP arrays, check if any of the counts is greater than maxLen and update maxLen accordingly.
  6. Return maxLen as the result.
UML Thumbnail

Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...