329. 矩阵中的最长递增路径

给定一个整数矩阵,找出最长递增路径的长度。

对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。

示例 1:

输入: nums =

[

[9,9,4],

[6,6,8],

[2,1,1]

]

输出: 4

解释: 最长递增路径为 [1, 2, 6, 9]。

示例 2:

输入: nums =

[

[3,4,5],

[3,2,6],

[2,2,1]

]

输出: 4

解释: 最长递增路径是 [3, 4, 5, 6]。注意不允许在对角线方向上移动。

class Solution {
public int longestIncreasingPath(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int max = 0;
int row = matrix.length;
int col = matrix[0].length;
int[][] dp = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
max = Math.max(max, loop(matrix, Integer.MIN_VALUE, dp, i, j)); //从每个节点开始搜索
}
}
return max;
}
/**
* 记忆化搜索
* @param mat 数据矩阵
* @param pre 路径中的前一个结点数字
* @param dp 保存从每个结点开始搜索的最长升序序列的长度
* @param i 当前位置
* @param j 当前位置
* @return
*/
private int loop(int[][] mat, int pre, int[][] dp, int i, int j) {
if (i < 0 || j < 0 || i >= mat.length || j >= mat[0].length || mat[i][j] <= pre) { //停止搜索条件
return 0;
}
if (dp[i][j] != 0) { //如果有数据,直接返回
return dp[i][j];
}
int max = 0; //进行搜索
max = Math.max(max, loop(mat, mat[i][j], dp, i - 1, j));
max = Math.max(max, loop(mat, mat[i][j], dp, i + 1, j));
max = Math.max(max, loop(mat, mat[i][j], dp, i, j - 1));
max = Math.max(max, loop(mat, mat[i][j], dp, i, j + 1));
dp[i][j] = max + 1;
return dp[i][j];
}
}

Java实现 LeetCode 329 矩阵中的最长递增路径的更多相关文章

  1. Leetcode 329.矩阵中的最长递增路径

    矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: n ...

  2. Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

    Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...

  3. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  4. LeetCode. 矩阵中的最长递增路径

    题目要求: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例: 输入: nums = [ ...

  5. [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

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

  7. 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919

    大致题意: Description 难怪Michael喜欢滑雪,因为滑雪确实很刺激.为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一 ...

  8. Java实现 LeetCode 124 二叉树中的最大路径和

    124. 二叉树中的最大路径和 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: ...

  9. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

随机推荐

  1. .NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)

    依赖倒置原则(DIP) 依赖倒置(Dependency Inversion Principle,缩写DIP)是面向对象六大基本原则之一.他是指一种特定的的解耦形式,使得高层次的模块不依赖低层次的模块的 ...

  2. 真正免费,不限页数的PDF转Word工具

    真正免费,不限页数的PDF转Word工具 我们知道PDF转Word工具非常多,但大部分都有各种限制,限大小,限页数,加水印等等. 这其中绝大部分其实并不能做到格式完全一样,遇到图片更是直接傻了. 我们 ...

  3. Intellij Idea2018破解教程(激活到2099年)

    1.下载IntelliJ IDEA 2018.1.6 链接:https://pan.baidu.com/s/18ZcKiPp3LU5S-la10r5icw 提取码:ghn0 2.安装IntelliJ ...

  4. Dockerfile-Namespace

    Docker核心-Namespaces(命名空间) 1)概念: 命令空间是Linux内核的一个强大的特性.每个容器都有自己单独的命令空间,运行在其中的应用都是独立在操作系用中运行一样.命名空间保证了容 ...

  5. 【图论算法】LCA最近公共祖先问题

    LCA模板题https://www.luogu.com.cn/problem/P3379题意理解 对于有根树T的两个结点u.v,最近公共祖先LCA(u,v)表示一个结点x,满足x是u.v的祖先且x的深 ...

  6. Jenkins 实现 ldap认证

    使用自己搭建的openldap: 使用Test LdapSetting测试的结果: 所测试的用户在:svn,jenkins,gitlab,sonarqube,wpsadmin组下 若用户不在jenki ...

  7. 【Leetcode】1340. Jump Game V 【动态规划/记忆性搜索】

    Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + ...

  8. Unity2D模拟控制位移

    using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { public ...

  9. P2444 [POI2000]病毒 AC自动机

    P2444 [POI2000]病毒 #include <bits/stdc++.h> using namespace std; ; struct Aho_Corasock_Automato ...

  10. nginx操作目录

    nginx配置文件/conf/nginx.conf 错误日志功能:los/error.log 访问日志功能:logs/access.log 站点服务请求功能配置:html/ 禁止访问功能配置 404页 ...