方法1:暴力递归,这个很简单,就是对每一个位置求最大递增路径,然后在看谁最大,单独对每一个位置,可以先递归看是否可左,上,下,右,以及如果可以走的最大递增路径,然后取最大值即是所求,代码很简单,没什么难扣的边界,关键是这个方法是超时的,不行的,因为存在大量重复的递归操作,所以引入方法2,记忆化搜索,我感觉这个题貌似没办法转dp,因为怎么确定初始的值呢。。。
更正:可以dp,哎,奈何我如此菜,根本没想到。。
暴力递归的化时间复杂度,每个点四个可能性,4的m*n?????,不能把
class Solution {
public int longestIncreasingPath(int[][] matrix) {
int max=0;
if(matrix.length==0||matrix[0].length==0) return 0;
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++) max=Math.max(dfs(matrix,i,j),max);
}
return max;
}
public int dfs(int[][] matrix,int row,int col){
//初始只有自己一定最少是1.
int path=1;
//能否向左走
if(col>0&&matrix[row][col-1]>matrix[row][col]) path=Math.max(path,dfs(matrix,row,col-1)+1);
//能否向上走
if(row>0&&matrix[row-1][col]>matrix[row][col]) path=Math.max(path,dfs(matrix,row-1,col)+1);
//能否向右走
if(col<matrix[0].length-1&&matrix[row][col+1]>matrix[row][col]) path=Math.max(path,dfs(matrix,row,col+1)+1);
//能否向下走
if(row<matrix.length-1&&matrix[row+1][col]>matrix[row][col]) path=Math.max(path,dfs(matrix,row+1,col)+1);
return path;
}
}
方法2:记忆化搜索,时间复杂度mn(就是计算mn个点,都是直接取值的,所以每个点是o1)
class Solution {
public int longestIncreasingPath(int[][] matrix) {
int max=0;
if(matrix.length==0||matrix[0].length==0) return 0;
int[][] jiyi=new int[matrix.length][matrix[0].length];//记忆化数组
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
max=Math.max(dfs(matrix,i,j,jiyi),max);
}
}
return max;
}
public int dfs(int[][] matrix,int row,int col,int[][] jiyi){
int path=1;
//能否向左走
if(col>0&&matrix[row][col-1]>matrix[row][col]){
if(jiyi[row][col-1]!=0) path=Math.max(path,jiyi[row][col-1]+1);
else path=Math.max(path,dfs(matrix,row,col-1,jiyi)+1);
}
//能否向上走
if(row>0&&matrix[row-1][col]>matrix[row][col]){
if(jiyi[row-1][col]!=0) path=Math.max(path,jiyi[row-1][col]+1);
else path=Math.max(path,dfs(matrix,row-1,col,jiyi)+1);
}
//能否向右走
if(col<matrix[0].length-1&&matrix[row][col+1]>matrix[row][col]){
if(jiyi[row][col+1]!=0) path=Math.max(path,jiyi[row][col+1]+1);
else path=Math.max(path,dfs(matrix,row,col+1,jiyi)+1);
}
//能否向下走
if(row<matrix.length-1&&matrix[row+1][col]>matrix[row][col]){
if(jiyi[row+1][col]!=0) path=Math.max(path,jiyi[row+1][col]+1);
else path=Math.max(path,dfs(matrix,row+1,col,jiyi)+1);
}
jiyi[row][col]=path;
return path;
}
}
动规左神版本:(就是记忆化搜索)
public static int longestIncreasingPath(int[][] m) {
if (m == null || m.length == 0 || m[0].length == 0) {
return 0;
}
int[][] h = new int[m.length][m[0].length];
int max = 0;
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length; j++) {
max = Math.max(max, maxIncrease(m, h, i + 1, j, m[i][j]) + 1);
max = Math.max(max, maxIncrease(m, h, i, j + 1, m[i][j]) + 1);
max = Math.max(max, maxIncrease(m, h, i - 1, j, m[i][j]) + 1);
max = Math.max(max, maxIncrease(m, h, i, j - 1, m[i][j]) + 1);
}
}
return max;
}
public static int maxIncrease(int[][] m, int[][] h, int i, int j, int p) {
if (i < 0 || i >= m.length || j < 0 || j >= m[0].length || m[i][j] >= p) {
return 0;
}
if (h[i][j] == 0) {
h[i][j] = maxIncrease(m, h, i + 1, j, m[i][j]) + 1;
h[i][j] = Math.max(h[i][j], maxIncrease(m, h, i, j + 1, m[i][j]) + 1);
h[i][j] = Math.max(h[i][j], maxIncrease(m, h, i - 1, j, m[i][j]) + 1);
h[i][j] = Math.max(h[i][j], maxIncrease(m, h, i, j - 1, m[i][j]) + 1);
}
return h[i][j];
}
英语看的:力扣的一个人写的dp
public int longestIncreasingPath(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0)
return 0;
//元素入最小堆,数组下标0-value,数组下标1-matrix中y坐标,数组下标2-matrix中x坐标,最小堆以value排序
Queue<int[]> minDump = new PriorityQueue<int[]>((pre,next) -> pre[0] - next[0]);
for(int y = 0; y < matrix.length; y++){
for(int x = 0; x < matrix[0].length; x++){
minDump.offer(new int[]{matrix[y][x],y,x});
}
}
//dp(记录当前元素累积到的最大递增路径长度)
int[][] dp = new int[matrix.length][matrix[0].length];
//实时记录最大路径,作为返回值返回
int maxLength = 0;
//元素从小到大开始dp(保证大的元素排在小的元素后被累积)
while(minDump.size() > 0){
int[] curElement = minDump.poll();
int value = curElement[0];
int y = curElement[1];
int x = curElement[2];
int curMax = 1;
//四个方向比较最大路径(如果matrix元素大于周边的元素,则最长路径在周边dp的基础上+1)
if(y > 0 && value > matrix[y - 1][x])
curMax = Math.max(curMax,dp[y - 1][x] + 1);
if(y < matrix.length - 1 && value > matrix[y + 1][x])
curMax = Math.max(curMax,dp[y + 1][x] + 1);
if(x > 0 && value > matrix[y][x - 1])
curMax = Math.max(curMax,dp[y][x - 1] + 1);
if(x < matrix[0].length - 1 && value > matrix[y][x + 1])
curMax = Math.max(curMax,dp[y][x + 1] + 1);
//累积dp
dp[y][x] = curMax;
//实时记录最大值
maxLength = Math.max(maxLength,curMax);
}
return maxLength;
}