329. Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path.
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
Example 1:
nums = [ [9,9,4], [6,6,8], [2,1,1] ]Return
4
The longest increasing path is
[1, 2, 6, 9]
.
Analysis:
given the two-dimensional integer array, we need to find the Longest Increasing Path in a Matrix. we can move to four directions, up down right and left.
The first method is DFS, however it takes too much time, IF we use DFS, we need to start with all positions in this array and continue for the whole
array.
code:
DFS, I did not check the correcness of this method
public class Solution {
int maxNum = Integer.MIN_VALUE;
public int longestIncreasingPath(int[][] matrix) {
int rowNum = matrix.length;
if(rowNum == 0)
return 0;
int columnNum = matrix[0].length;
if(column == 0)
return 0;
int row = 0;
int col = 0;
int number = 0;
for(int i = 0; i < rowNum;i++){
for(int j = 0; j < columnNum; j++){
helper(i,j,matirx,0)
}
}
return maxNum;
}
//This is the depth first search
public int helper(int row, int col, int[][] matrix,int number){
number++;
if(number > maxNum )
maxNum = number;
// if(number == 1)
// {
// lastOne = matrix[row, col];
int rowNum = matrix.length;
int colNum = matrix[0].length;
int temNum = number++;
if(row - 1 >=0 && matrix[row - 1][col] > matrix[row][col]){
int val = helper(row - 1,col,matrix,number);
if(val > maxNum)
maxNum = number;
if(val > temNum)
temNum = val;
}
if(row + 1 < rowNum && matrix[row + 1][col] > matrix[row][col]){
int val = helper(row + 1, col, matrix, number);
if(val > maxNum)
maxNum = number;
if(val > temNum)
temNum = val;
}
if(col - 1 >=0 && matrix[row][col - 1] > matrix[row][col]){
int val = helper(row, col - 1, matrix, number);
if(val > maxNum)
maxNum = number;
if(val > temNum)
temNum = val;
}
if(col + 1 >=colNum && matrix[row][col + 1] > matrix[row][col]){
int val = helper(row, col + 1, matrix,number);
if(val > maxNum)
maxNum = number;
if(val > temNum)
temNum = val;
}
return temNum;
}
}
Dynamic programming + DFS.
public class Solution {
int maxNum = Integer.MIN_VALUE;
int[][] record;
public int longestIncreasingPath(int[][] matrix) {
int rowNum = matrix.length;
if(rowNum == 0)
return 0;
int colNum = matrix[0].length;
if(colNum == 0)
return 0;
record= new int[rowNum][colNum];
//initialized the record array to all 0;
for(int i = 0; i < rowNum; i++){
for(int j = 0; j < colNum; j++){
record[i][j] = 0;
}
}
int row = 0;
int col = 0;
for(int i = 0; i < rowNum;i++){
for(int j = 0; j < colNum; j++){
helper(i,j,matrix);
}
}
return maxNum;
}
//This is the depth first search
public int helper(int row, int col, int[][] matrix){
if(record[row][col] != 0)
return record[row][col];
// int number = 1;
// number++;
// if(number > maxNum )
// maxNum = number;
// if(number == 1)
// {
// lastOne = matrix[row, col];
int rowNum = matrix.length;
int colNum = matrix[0].length;
int temNum = 1;
if(row - 1 >=0 && matrix[row - 1][col] > matrix[row][col]){
int val = 1 + helper(row - 1,col,matrix);
if(val > maxNum)
maxNum = val;
if(val > temNum)
temNum = val;
}
if(row + 1 < rowNum && matrix[row + 1][col] > matrix[row][col]){
int val = 1 + helper(row + 1, col, matrix);
if(val > maxNum)
maxNum = val;
if(val > temNum)
temNum = val;
}
if(col - 1 >=0 && matrix[row][col - 1] > matrix[row][col]){
int val = 1 + helper(row, col - 1, matrix, number);
if(val > maxNum)
maxNum = val;
if(val > temNum)
temNum = val;
}
if(col + 1 < colNum && matrix[row][col + 1] > matrix[row][col]){
int val = 1 + helper(row, col + 1, matrix,number);
if(val > maxNum)
maxNum = val;
if(val > temNum)
temNum = val;
}
if(temNum > maxNum )
maxNum = temNum;
record[row][col] = temNum;
return temNum;
// else{
}
}
Lesson learned
I learn from this program that when do the dynamic programming, you need to consider whether or not to store the computation result in a array, so that you can avoid repeated computation. This may be called purnning.