Longest Line of Consecutive One in Matrix in C++

C++Server Side ProgrammingProgramming

Suppose we have one binary matrix M, we have to find the longest line of consecutive one in that matrix. The line could be horizontal, vertical, diagonal or anti-diagonal.

So, if the input is like

0110
0110
0001

then the output will be 3

To solve this, we will follow these steps −

  • ret := 0

  • n := row of M

  • m := column of M

  • Define one 3D array dp of order n x m x 4

  • for initialize i := 0, when i < m, update (increase i by 1), do −

    • for initialize j := 0, when j < 4, update (increase j by 1), do −

      • dp[0, i, j] := M[0, i]

      • ret := maximum of ret and dp[0, i, j]

  • for initialize j := 0, when j < m, update (increase j by 1), do −

    • if M[0, j] is non-zero and j > 0, then −

      • dp[0, j, 1] := 1 + dp[0, j - 1, 1]

      • ret := maximum of ret and dp[0, j, 1]

  • for initialize i := 1, when i < n, update (increase i by 1), do −

    • for initialize j := 0, when j < m, update (increase j by 1), do −

      • dp[i, j, 0] := (if M[i, j] is non-zero, then 1 + dp[i - 1, j, 0], otherwise 0)

      • if j > 0, then −

        • dp[i, j, 1] := (if M[i, j] is non-zero, then dp[i, j - 1, 1] + 1, otherwise 0)

        • dp[i, j, 2] := (if M[i, j] is non-zero, then dp[i - 1, j - 1, 2] + 1, otherwise 0)

      • Otherwise

        • dp[i, j, 1] := M[i, j]

        • dp[i, j, 2] := M[i, j]

      • if j + 1 < m, then −

        • dp[i, j, 3] := (if M[i, j] is non-zero, then dp[i - 1, j + 1, 3] + 1, otherwise 0)

      • Otherwise

        • dp[i, j, 3] := M[i, j]

      • for initialize k := 0, when k < 4, update (increase k by 1), do −

        • ret := maximum of ret and dp[i, j, k]

  • return ret

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   int longestLine(vector<vector<int>>& M) {
      int ret = 0;
      int n = M.size();
      int m = !n ? 0 : M[0].size();
      vector<vector<vector<int> > > dp(n, vector<vector<int> >(m, vector<int>(4)));
      for (int i = 0; i < m; i++) {
         for (int j = 0; j < 4; j++) {
            dp[0][i][j] = M[0][i];
            ret = max(ret, dp[0][i][j]);
         }
      }
      for (int j = 0; j < m; j++) {
         if (M[0][j] && j > 0) {
            dp[0][j][1] = 1 + dp[0][j - 1][1];
            ret = max(ret, dp[0][j][1]);
         }
      }
      for (int i = 1; i < n; i++) {
         for (int j = 0; j < m; j++) {
            dp[i][j][0] = M[i][j] ? 1 + dp[i - 1][j][0] : 0;
            if (j > 0) {
               dp[i][j][1] = M[i][j] ? dp[i][j - 1][1] + 1 : 0;
               dp[i][j][2] = M[i][j] ? dp[i - 1][j - 1][2] + 1 : 0;
            }
            else {
               dp[i][j][1] = M[i][j];
               dp[i][j][2] = M[i][j];
            }
            if (j + 1 < m) {
               dp[i][j][3] = M[i][j] ? dp[i - 1][j + 1][3] + 1 : 0;
            }
            else {
               dp[i][j][3] = M[i][j];
            }
            for (int k = 0; k < 4; k++) {
               ret = max(ret, dp[i][j][k]);
            }
         }
      }
      return ret;
   }
};
main(){
   Solution ob;
   vector<vector<int>> v = {{0,1,1,0},{0,1,1,0},{0,0,0,1}};
   cout << (ob.longestLine(v));
}

Input

{{0,1,1,0},{0,1,1,0},{0,0,0,1}}

Output

3
raja
Updated on 16-Nov-2020 14:14:24

Advertisements