
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Longest Line of Consecutive One in Matrix in C++
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
0 | 1 | 1 | 0 |
0 | 1 | 1 | 0 |
0 | 0 | 0 | 1 |
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 −
#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
- Related Articles
- Finding longest line of 1s in a matrix in JavaScript
- Longest Consecutive Sequence in Python
- Finding longest consecutive joins in JavaScript
- Longest string consisting of n consecutive strings in JavaScript
- Binary Tree Longest Consecutive Sequence in C++
- Program to find length of longest consecutive sequence in Python
- Binary Tree Longest Consecutive Sequence II in C++
- Length of the longest possible consecutive sequence of numbers in JavaScript
- Find longest consecutive letter and digit substring in Python
- Longest Increasing Path in a Matrix in Python
- Program to find longest consecutive run of 1s in binary form of n in Python
- Finding the longest consecutive appearance of a character in another string using JavaScript
- Program to find length of longest consecutive sublist with unique elements in Python
- Program to find length of longest consecutive path of a binary tree in python
- Finding the longest common consecutive substring between two strings in JavaScript
