leetcode 329. Longest Increasing Path in a Matrix(矩阵中的最长递增路径)

【脚本篇】---spyglass lint脚本 【代码】【脚本篇】---spyglass lint脚本。 阅读详情

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:

Input: nums = 
[
  [9,9,4],
  [6,6,8],
  [2,1,1]
] 

Output: 4
Explanation: The longest increasing path is [1, 2, 6, 9].

思路:
直接用DFS遍历所有路径会TLE
所以用dp数组储存之前DFS的结果,矩阵中一个点(x, y)的最长递增路径相当于它上下左右的四个相邻点的路径长+1
走到一个(x, y)点时,首先把当前路径长设为1,然后根据相邻的4个点进行更新,而且,因为是递增path,所以要判断原矩阵中相邻点处的值>当前点处的值时才算递增,<=当前点处的值时路径长直接就是1。还要判断是否超出边界。

dfs计算本身会遍历到所有的dp元素,主函数中遍历matrix是取dp中所有结果的最大值,也就是最长的递增路径长度。

class Solution {
    private int rows = 0;
    private int cols = 0;
    private int[][] dp;
    public int longestIncreasingPath(int[][] matrix) {
        if(matrix == null || matrix.length == 0) {
            return 0;
        }
        
        rows = matrix.length;
        cols = matrix[0].length;
        dp = new int[rows][cols];
        //for(int i = 0; i < rows; i++) {
        //    Arrays.fill(dp[i], -1);
        //}
              
        int result = 0;
        
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                result = Math.max(result, dfs(matrix, i, j));
            }
        }
        
        return result;
    }
    
    public int dfs(int[][] matrix, int row, int col) {
        if(dp[row][col] != 0) {
            return dp[row][col];
        }
        
        int[] offset = new int[]{-1, 0, 1, 0, -1};
        int nrow = 0;   //neighbor x
        int ncol = 0;   //neighbor y
        
         
        dp[row][col] = 1;
        for(int i = 0; i < 4; i++) {
            ncol = col + offset[i];
            nrow = row + offset[i + 1];
                    
            if(ncol < 0 || nrow < 0 || ncol >= cols || nrow >= rows ||
                     matrix[nrow][ncol] <= matrix[row][col]) {
                continue;
            }
                    
            dp[row][col] = Math.max(dp[row][col], 1 + dfs(matrix, nrow, ncol));
        } 
        
        return dp[row][col];
    }
}
【YOLOv8改进[Conv]】使用OREPA助力V8更优秀 使用OREPA助力V8更优秀,降低训练成本,在不引入任何推理时间成本的情况下提高深度模型的性能。 阅读详情

相关推荐

Ollama模型路径迁移实战:Windows/Mac/Linux三平台保姆级教程(附常见问题排查)

本文提供了一份详细的Ollama模型路径迁移教程,涵盖Windows、macOS和Linux三大平台。通过设置OLLAMA_MODELS环境变量或创建符号链接,指导用户将模型从默认路径安全迁移至新位置,以释放系统盘空间或提升性能。教程包含迁移前的准备、分平台步骤、迁移后的验证以及常见问题排查,确保迁移过程平滑可靠。

js777的博客 1188

【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/ 题目描述: Given an integer matrix, find the length of the...

负雪明烛 3849

QT触摸屏开发:如何通过重写事件处理实现精准按键长按检测

本文针对QT触摸屏开发中按键长按检测不精准的常见问题,深入剖析了传统信号机制失效的原因,并提供了通过重写事件处理函数(event)的解决方案。文章详细介绍了创建自定义TouchButton类、直接处理QTouchEvent以实现可靠长按检测的完整步骤与代码示例,并分享了进阶优化与避坑指南,帮助开发者实现跨平台一致的精准交互。

weixin_29207149的博客 119

leetcode学习笔记57

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

weixin_38941866的博客 225

光电容积脉搏波PPG信号分析笔记

脉搏波信号PPG分析笔记

teresa_zp的专栏 6489

自适应粒子群优化(Adaptive Particle Swarm Optimization,APSO

下面是一个关于自适应粒子群优化(Adaptive Particle Swarm Optimization,APSO)的博客,希望可以帮助您。提示:以下是本篇文章正文内容,下面案例可供参考在上面的代码中,表示粒子的数量,表示解空间的维度,表示迭代次数,表示优化的目标函数。在__init__方法中,我们初始化了粒子群的位置和速度,并将每个粒子的位置作为其最优解,将其适应度值初始化为正无穷。我们还初始化了学习因子和惯性权重的参数。在run方法中,我们迭代了次,计算每个粒子的适应度值和最优解。

weixin_52026057的博客 9925

LeetCode知识点总结 - 73

LeetCode 73. Set Matrix Zeroes

m0_59773145的博客 221

python提取图片中的文字写入excel文件,并打包为exe可执行文件

python提取图片中的文字写入excel文件,并打包为exe可执行文件

liqiang12689的博客 993

leetcode329 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 o...

better_girl的博客 217

【C#程序员入门AI】必知的一些知识

2026年C#调用AI,核心就4个概念+3种方式4个概念:大模型(大脑)、嵌入(数字密码)、向量(载体)、Agent(超级助理)3种方式:云端API(快)、本地模型(隐私)、混合部署(最佳)不用怕复杂,跟着这个思路走,你就能从“AI小白”变成“C# AI开发能手”,把AI集成到你的.NET项目里,提升效率、创造价值!

人工智能AI技术 413

基于高斯两步移动搜寻法(2SFCA)的城市绿地可达性分析

第二步,对于每一个街道i,给定空间距离阈值d0,便形成另一个空间作用域,同样对于落在空间作用域内的每块绿地l的供给比率(Rl)利用高斯方程赋以权重,然后对这些加权后的供给比率(Rl)进行加和,便得到每个街道i的绿地可达性Ai。图3与图4是高斯两步移动搜索法的示例。在图3中,以d0为阈值,有1、2、3 三个街道中心点落在了绿地a的空间作用域内。同样在图4中,以d0为阈值,有a、c两个绿地中心点落在了街道2的空间作用域内,根据公式(3),对a、c高斯方程加权后的供需比率进行累加,便得到了街道2 的绿地可达性。

2301_77310370的博客 2407

Java二维数组递减最长路径_[LeetCode] 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 ...

weixin_42116672的博客 235

基于 ESP32 的分布式光伏微电网智能监控系统设计与实现

设备 / 模块连接 ESP32 引脚备注I2C 总线(SDA)GPIO21所有 I2C 设备共用I2C 总线(SCL)GPIO22所有 I2C 设备共用DS18B20(单总线)GPIO18单总线通信,需外接 4.7K 上拉电阻有源蜂鸣器GPIO19低电平触发,串联 100Ω 限流电阻ESP32 供电5V/GND由 LM2596 输出 5V 供电,或直接 USB 供电(调试阶段)INA219(光伏)光伏阵列正负极接 INA219 的 VIN+/VIN-

weixin_42300449的博客 310

Leetcode 329.矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

Leetcode 329.矩阵中的最长递增路径 1 题目描述(Leetcode题目链接)   给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9]。 输入: nums = [ [3,4,5], [3,2,6], [2,2,1] ]

qq_39378221的博客 310

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LeetCode-329 Longest Increasing Path in a Matrix | 矩阵中的最长递增路径

LeetCode-329 Longest Increasing Path in a Matrix | 矩阵中的最长递增路径 题目描述 给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9]。 示例 2: 输入: nums = [ [3,4,5], [3,2

weixin_43265151的博客 381

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

LeetCode 329. Longest Increasing Path in a Matrix(矩阵内的最长递增路径)

原题网址:https://leetcode.com/problems/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 dir

James Pan 1739

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode(329):矩阵中的最长递增路径 Longest Increasing Path in a Matrix(Java)

2019.9.29 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) github:https://github.com/ChopinXBP/LeetCode-Babel 这一题乍一看好像直接上DFS,但是很容易会超时。可以考虑复用已经计算过的结果,用记忆化DFS的方法做(也是效率最高的方法)。 另外还有一种比较巧妙的方法:动态规划+拓扑排序。 1.在matrix外围加上...

NJU_ChopinXBP的博客 386

【LeetCode】329. Longest Increasing Path in a Matrix 矩阵中的最长递增路径(Hard)(JAVA)

【LeetCode】329. Longest Increasing Path in a Matrix 矩阵中的最长递增路径(Hard)(JAVA) 题目地址: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ 题目描述: Given an integer matrix, find the length of the longest increasing path. From each cell, you can either

qq_16927853的博客 185

【leetcode】329. 矩阵中的最长递增路径(longest-increasing-path-in-a-matrix)(DFS)[困难]

链接 https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix/ 耗时 解题:2 h 题解:7 min 题意 给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。 思路 暴力深搜+记忆化。 从矩阵的每个位置开始 按递增 深搜四个方向,并每次更新递增路径的长度。如果之前到达过当前位置,将记忆的长度与当前长度比较,如果记忆的长度大于

Krone_的博客 224

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

Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。 示例 1: 输入: nums...

denghan1065的博客 254

# 329. Longest Increasing Path in a Matrix

文章目录329. Longest Increasing Path in a Matrix解题代码解题思路 329. Longest Increasing Path in a Matrix 英文题目链接 中文题目链接 Given an m x n integers matrix, return the length of the longest increasing path in matrix. From each cell, you can either move in four directions:

sun%moon 386

Java二维数组递减最长路径_Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)...

Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)深度优先搜索的解题详细介绍,点击给定一个整数矩阵,找出最长递增路径的长度。对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。示例 1:输入: nums =[[9,9,4],[6,6,8],[...

weixin_30764045的博客 264

【Leetcode】Longest Increasing Path in a Matrix

题目链接:https://leetcode.com/problems/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 fou

业精于勤,荒于嬉 2877

LeetCode //C - 329. Longest Increasing Path in a Matrix

【代码】LeetCode //C - 329. Longest Increasing Path in a Matrix。

Made in Code 2573

[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

coder 进阶的专栏 2254

leetcode 329 Longest Increasing Path in a Matrix

由于我要求最长路径,所以我需要一个数组来保存每个点的最长路径,然后遍历所有的点就可以了。dfs的定义是,返回当前节点最长的递增路径。然后当前节点的最长递增路径,是周围节点递增路径的最大值,就可以用dfs了。分析:首先这题目和word search一样都是可以用dfs的并且这里不需要我有vis数组,因为数组是绝对不会往回走的。因为记忆化是你这边已经走过我就记录下来,按照我这样的写法,这完全没有记录下来,每次都要重新计算已经计算过的地方。问题:为什么我这样写记忆化就有问题?题意:找到最长的递增路径。

xxxmmc的博客 373
上一篇: leetcode 164. Maximum Gap(最大间距)
下一篇: leetcode 394. Decode String(解码字符串)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址