Java实现 LeetCode 329 矩阵中的最长递增路径
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 矩阵中的最长递增路径的更多相关文章
- Leetcode 329.矩阵中的最长递增路径
矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: n ...
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- [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 ...
- LeetCode. 矩阵中的最长递增路径
题目要求: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例: 输入: nums = [ ...
- [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 ...
- 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 ...
- 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919
大致题意: Description 难怪Michael喜欢滑雪,因为滑雪确实很刺激.为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一 ...
- Java实现 LeetCode 124 二叉树中的最大路径和
124. 二叉树中的最大路径和 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
随机推荐
- Redis学习笔记(二) 链表
链表提供了高效的节点重排能力,以及顺序性的节点访问方式,并且可以通过增删节点来灵活地调整链表的长度. redis中链表应用广泛,如list中就使用了链表. 每一个链表节点使用listNode结构标识( ...
- xml(2)
DTD用来验证xml的形式 引入格式:<!DOCTYPE 根元素的名字 SYSTEM "路径">//其中system代表本地路径 <?xml version=&q ...
- 最近常问的99道Java多线程面试题 !
前言 今天给大家更新的是一篇关于多线程面试的文章,也是霸哥根据时下热门的面试内容给大家进行总结的, 本篇文章属于干货内容! 请各位读者朋友一定要坚持读到最后,完整阅读本文后相信你对多线程会有不一样感悟 ...
- 应用视觉设计——CSS实现线性渐变效果
在freeCodeCamp中应用视觉设计的课程中,介绍了一种通过使用CSS属性实现线性渐变效果. 1.线性渐变: background:linear-gredient(gradient-directi ...
- 小程序-云开发 bindscroll滚动事件执行setData()方法,导致scroll-view视图抖动
需求描述 想做一个类似京东小程序首页功能列表左右滑动的效果,效果图如下 遇到的问题 1. 如何让scroll-view显示两行 做过小程序开发的都知道,scroll-view要么显示一行,可以左右滚动 ...
- express+mysql实现简单的登录功能
登录页面图: node.js文件代码: const express=require("express"); const app=express(); const path=requ ...
- 微信小程序通信录
第一步:phone.wxml中 <view bindlongtap="clickPhone">{{phoneNum}}</view> 第二步:phone.j ...
- Centos7安装jupyter notebook
安装python3 查看当前python版本 [root@iz1i4qd6oynml0z /]# python -V Python 2.7.5 安装python3以及检查python3的版本 yum ...
- 树形DP基础题 HDU1520
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- queue.Queue()
一.构造方法 Queue是构造方法,函数签名是Queue(maxsize=0) ,其中maxsize设置队列的大小. 二.实例方法 Queue.qsize(): 返回queue的近似值.注意:qsiz ...