CC-13 : Find longest incremental path.
Description:
Given an adjacency matrix, find the length of longest path of incrementing numbers. The path can only travel in directions left, right, up down of cells sharing a boundary but cannot travel diagonal wise.Test cases and expected outputs:
| Input Parameters | Expected outputs |
|---|---|
|
Matrix : 1,2,3, 4,6,7, 9,10,11, |
Longest increasing path: 5 |
Pseudocode:
findLongestIncPath (int[][] matrix) method:
| Initialize a variable named lngstIncPath to store the length of longest path found. |
| Set variable named rows to matrix.length. |
| Set variable named columns to matrix[0].length. |
| Initialize a 2 dimensional array called memorised with same dimensions as input matrix. |
| Iterate through vertices of matrix using a for loop with iIdx as loop variable with values from 0 to count of matrix.length:
Iterate through vertices of matrix using a for loop with jIdx as loop variable with values from 0 to count of matrix[0].length:
Set variable currlngstIncPath to return value from call to longestIncPathDfs (iIdx,jIdx, matrix, memorised, -1).
If currlngstIncPath > lngstIncPath then set lngstIncPath= currlngstIncPath.
|
| Return lngstIncPath. |
findIslandPerimeterDfs (int row, int col, int[][] matrix, int[][] memorized, int prevVal) method:
| if row and col are within index limits of matric and matrix[row][col]<=prevVal:
return 0.
|
| If visited[row][col]>1, return memorized[row][col]. |
| Initialise lngstIncPath=1. |
| Set prevVal= matrix[row][col]. |
| Set currlngstIncPath=0. |
| Set currlngstIncPath int variable to value return by recursive call to longestIncPathDfs (row+1,col, matrix, memorised, prevVal). |
| If currlngstIncPath > lngstIncPath then set lngstIncPath= currlngstIncPath. |
| Set currlngstIncPath int variable to value return by recursive call to longestIncPathDfs (row-1,col, matrix, memorised, prevVal). |
| If currlngstIncPath > lngstIncPath then set lngstIncPath= currlngstIncPath. |
| Set currlngstIncPath int variable to value return by recursive call to longestIncPathDfs (row,col-1, matrix, memorised, prevVal). |
| If currlngstIncPath > lngstIncPath then set lngstIncPath= currlngstIncPath. |
| Return lngstIncPath. |
Code:
public int longestIncPathDfs(int row, int col, int[][] matrix, int[][] memorised,int prevVal ){
if ((row < 0 ) || (row >= matrix.length)
|| (col < 0 ) ||(col >= matrix[0].length)
|| (matrix[row][col] <= prevVal)){
return 0;
}
if (memorised[row][col] > 0) {
return memorised[row][col];
}
int lngstIncPath=1;
prevVal=matrix[row][col];
int currlngstIncPath=0;
currlngstIncPath=1+longestIncPathDfs(row+1,col,matrix,memorised,prevVal);
if (currlngstIncPath > lngstIncPath){
lngstIncPath=currlngstIncPath;
}
currlngstIncPath=1+longestIncPathDfs(row,col+1,matrix,memorised,prevVal);
if (currlngstIncPath > lngstIncPath){
lngstIncPath=currlngstIncPath;
}
currlngstIncPath=1+longestIncPathDfs(row-1, col, matrix,memorised,prevVal);
if (currlngstIncPath > lngstIncPath){
lngstIncPath=currlngstIncPath;
}
currlngstIncPath=1+longestIncPathDfs(row, col-1, matrix,memorised,prevVal);
if (currlngstIncPath > lngstIncPath){
lngstIncPath=currlngstIncPath;
}
memorised[row][col]=lngstIncPath;
return lngstIncPath;
}
public int findLongestIncPath(int[][] matrix) {
int rows=matrix.length;
int cols=matrix[0].length;
int [][] memorised=new int[rows][cols];
int lngstIncPath=0;
for(int iIdx=0; iIdx < rows; iIdx++){
for(int jIdx=0; jIdx < cols;jIdx++){
int currlngstIncPath=longestIncPathDfs(iIdx,jIdx,matrix,memorised, -1);
if (currlngstIncPath > lngstIncPath){
lngstIncPath=currlngstIncPath;
}
}
}
return lngstIncPath;
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |