[LeetCode] 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,4],
[,6,8],
[,,1]
]
Return 4
The longest increasing path is [1, 2, 6, 9].
Example 2:
nums = [
[,,5],
[3,2,6],
[2,2,1]
]
Return 4
The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.
解法:
这道题给我们一个二维数组,让我们求矩阵中最长的递增路径,规定我们只能上下左右行走,不能走斜线或者是超过了边界。那么这道题的解法要用递归和DP来解,用DP的原因是为了提高效率,避免重复运算。我们需要维护一个二维动态数组dp,其中dp[i][j]表示数组中以(i,j)为起点的最长递增路径的长度,初始将dp数组都赋为0,当我们用递归调用时,遇到某个位置(x, y), 如果dp[x][y]不为0的话,我们直接返回dp[x][y]即可,不需要重复计算。我们需要以数组中每个位置都为起点调用递归来做,比较找出最大值。在以一个位置为起点用深度优先搜索DFS时,对其四个相邻位置进行判断,如果相邻位置的值大于上一个位置,则对相邻位置继续调用递归,并更新一个最大值,搜素完成后返回即可,参见代码如下:
public class Solution {
    public int longestIncreasingPath(int[][] matrix) {
        if (matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        int m = matrix.length, n = matrix[0].length;
        int res = 0;
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                res = Math.max(res, findLongestPath(matrix, dp, i, j));
            }
        }
        return res;
    }
    public int findLongestPath(int[][] matrix, int[][] dp, int i, int j) {
        dp[i][j] = 1;
        int[][] next = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for (int k = 0; k < next.length; k++) {
            int ni = i + next[k][0];
            int nj = j + next[k][1];
            if (ni >= 0 && ni < dp.length && nj >= 0 && nj < dp[0].length && matrix[ni][nj] > matrix[i][j]) {
                if (dp[ni][nj] != 0) {
                    dp[i][j] = Math.max(dp[i][j], dp[ni][nj] + 1);
                } else {
                    dp[i][j] = Math.max(dp[i][j], findLongestPath(matrix, dp, ni, nj) + 1);
                }
            }
        }
        return dp[i][j];
    }
}
[LeetCode] 329. Longest Increasing Path in a Matrix ☆☆☆的更多相关文章
- leetcode@ [329] Longest Increasing Path in a Matrix  (DFS + 记忆化搜索)
		
https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, find the ...
 - LeetCode #329. Longest Increasing Path in a Matrix
		
题目 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ...
 - [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question
		
在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.
 - 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
 - 329 Longest Increasing Path in a Matrix  矩阵中的最长递增路径
		
Given an integer matrix, find the length of the longest increasing path.From each cell, you can eith ...
 - [LeetCode] 329. Longest Increasing Path in a Matrix_Hard tag: Dynamic Programming, DFS, Memoization
		
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
 - 329. Longest Increasing Path in a Matrix(核心在于缓存遍历过程中的中间结果)
		
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
 - 329. Longest Increasing Path in a Matrix
		
最后更新 三刷? 找矩阵里的最长路径. 看起来是DFS,实际上也就是.但是如果从每个点都进行一次DFS然后保留最大的话,会超时. 这里需要结合DP,dp[i][j]表示以此点开始的最长路径,这样每次碰 ...
 - Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
		
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
 
随机推荐
- 利用Hibernate子查询(in) 得到部分字段(实体类的构造函数)
			
感人= = 终于弄好了 String hql="select new Shop(s.strid,s.shopname,s.tradearea,s.discountinfo,s.beginti ...
 - Hibernate主键注解
			
http://www.cnblogs.com/hongten/archive/2011/07/20/2111773.html 版权声明:本文为博主原创文章,未经博主允许不得转载.
 - 结对随即四则运算(带界面Java版)
			
//随机四则运算类 public class 随机四则运算 { public static void main(String[] args) { new 界面();//进入随机四则运算的首界面 } } ...
 - 第六周PSP&进度条
			
团队项目PSP 一.表格: C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论alpha完成情况并总结 9:40 11:20 17 ...
 - FastReport 变量列表使用
			
使用报表变量时,引用“frxVariables”单元. 变量定义在“TfrxVariable” 类: TfrxVariable = class(TCollectionItem) published p ...
 - 使用robot封装一个模拟键盘复制粘贴并按下回车的方法
			
/** * 复制数据到剪切板并粘贴出来并按下回车 * @param writeMe 需要粘贴的地址 * @throws java.awt.AWTException */ public void use ...
 - 10条SQL优化语句,让你的MySQL数据库跑得更快!
			
慢SQL消耗了70%~90%的数据库CPU资源: SQL语句独立于程序设计逻辑,相对于对程序源代码的优化,对SQL语句的优化在时间成本和风险上的代价都很低: SQL语句可以有不同的写法: 1 不使用子 ...
 - js学习1
			
js基础1: js组成: ECMAScript :解释器 .翻译 提供语言的基本功能 几乎没有兼容型问题 dom :document object model 有一些兼容型问题 bom :brower ...
 - delphi手动创建dataset并插入值
			
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...
 - RF相关知识
			
前言:下文中的总结都是来自于网络,有的来自与博客,有的来自于维基百科/百度百科,仅仅是为了方便查看. ASK: ASK:幅移键控调制的简写,例如二进制的,把二进制符号0和1分别用不同的幅度来表示, ...