leetcode 329 : Longest Increasing Path in a Matrix : dfs+dp

329. Longest Increasing Path in a Matrix

My Submissions
Total Accepted: 9010  Total Submissions: 29883  Difficulty: Hard

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].

Example 2:

nums = [
  [3,4,5],
  [3,2,6],
  [2,2,1]
]

Return 4
The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.



这道题目我使用的dfs+dp,这里dp的实现的方式是使用hashmap存储已经计算过的的节点,然后dfs搜索到已经计算过的结果,直接提取结果。
public class Solution {
    HashMap<Integer, Integer> index2lenth=new HashMap<>();
	public int Search(int iInex,int jIndex,int[][]matrix){
		int row=matrix.length;
		int column=matrix[0].length;
		if(index2lenth.containsKey(iInex*column+jIndex))
			return index2lenth.get(iInex*column+jIndex);
		int max=1;
		int temp;
		if(iInex-1>=0&&matrix[iInex-1][jIndex]<matrix[iInex][jIndex]){
			temp=Search(iInex-1, jIndex, matrix);
			max=max<(temp+1)?temp+1:max;
		}
		if(jIndex-1>=0&&matrix[iInex][jIndex-1]<matrix[iInex][jIndex]){
			temp=Search(iInex, jIndex-1, matrix);
			max=max<(temp+1)?temp+1:max;
		}
		if(iInex+1<row&&matrix[iInex+1][jIndex]<matrix[iInex][jIndex]){
			temp=Search(iInex+1, jIndex, matrix);
			max=max<(temp+1)?temp+1:max;
		}
		if(jIndex+1<column&&matrix[iInex][jIndex+1]<matrix[iInex][jIndex]){
			temp=Search(iInex, jIndex+1, matrix);
			max=max<(temp+1)?temp+1:max;
		}
		index2lenth.put(iInex*column+jIndex, max);
		return max;
	}
	public int longestIncreasingPath(int[][] matrix) {
		if(matrix.length==0)
			return 0;
		
		int max=0;
		int row=matrix.length;
		int column=matrix[0].length;
		
		for(int i=0;i<row;i++){
			for(int j=0;j<column;j++){
				if(index2lenth.containsKey(i*column+j)){
					int temp=index2lenth.get(i*column+j);
					max=max<temp?temp:max;
				}
				else {
					int temp=Search(i,j,matrix);
					max=max<temp?temp:max;
				}
			}
		}
		return max;
	}
}

总觉得有纯dp的方式,尝试了一下,超时了,求指导~
public class Solution {
    public int longestIncreasingPath(int[][] matrix) {
		if(matrix.length==0)
			return 0;
		int rowNum=matrix.length;
		int column=matrix[0].length;
		int[][] lenth=new int[rowNum][column];
		for(int i=0;i<rowNum;i++)
			for(int j=0;j<column;j++)
				lenth[i][j]=1;
		int count;
		int max=0;
		for(int k=0;k<column*rowNum-1;k++){
			count=0;
			for(int i=0;i<rowNum;i++){
				for(int j=0;j<column;j++){
					if(i-1>=0&&matrix[i-1][j]<matrix[i][j]){
						lenth[i][j]=lenth[i-1][j]+1;
						count++;
						max=max<lenth[i][j]?lenth[i][j]:max;
					}
					if(j-1>=0&&matrix[i][j-1]<matrix[i][j]){
						lenth[i][j]=lenth[i][j-1]+1;
						count++;
						max=max<lenth[i][j]?lenth[i][j]:max;
					}
					
					if(i+1<rowNum&&matrix[i+1][j]<matrix[i][j]){
						lenth[i][j]=lenth[i+1][j]+1;
						count++;
						max=max<lenth[i][j]?lenth[i][j]:max;
					}
					if(j+1<column&&matrix[i][j+1]<matrix[i][j]){
						lenth[i][j]=lenth[i][j+1]+1;
						count++;
						max=max<lenth[i][j]?lenth[i][j]:max;
					}
				}
			}
			if(count==0)
				break;
		}
		return max;
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值