Leetcode之深度优先搜索(DFS)专题-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],
  [2,1,1]
]
输出: 4
解释: 最长递增路径为 [1, 2, 6, 9]

示例 2:

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

分析:给定一个地图,求最长上升路径的长度。

首先会想到用DFS,遍历所有地图的节点,开始上下左右走,但这样会出现一些问题:举个例子:
1 2 3
4 5 6
7 8 9

这个例子的答案是9,而DFS的遍历过程是这样的:

1-9,2-9,3-9,4-9,5-9,6-9,7-9,8-9

一共会走八条路,这样一旦当数据量大的时候,就会超时,那我们引入记忆二维数组memo,

memo数组存储着当前这个点的最大递增长度,那我们在执行1-9的过程中,就可以把memo全部计算出来了,

在走后面的路径的时候,走到2发现memo存储着2这个点的最大长度,函数就可以直接返回memo的值了,后面的路也不用搜索了,节省了很多时间。

AC代码:

class Solution {
    public int longestIncreasingPath(int[][] matrix) {
        int ans = 0;
        if(matrix==null || matrix.length==0) return 0;
        int memo[][] = new int[matrix.length][matrix[0].length];
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length;j++){
                ans=Math.max(ans, dfs(matrix,i,j,memo));
            }
        }
        return ans;
    }
    int dirx[] = {0,0,1,-1};
    int diry[] = {1,-1,0,0};
    private int dfs(int[][] matrix,int x, int y,int[][] memo) {
        if(memo[x][y] != 0){
            return memo[x][y];
        }
        for(int i=0;i<4;i++){
            int xx = x+dirx[i];
            int yy = y+diry[i];
            if(xx>=0 && yy>=0 && xx<matrix.length && yy<matrix[0].length && matrix[xx][yy]>matrix[x][y]){
                memo[x][y] = Math.max(memo[x][y], dfs(matrix,xx,yy,memo));
            }
        }
        return ++memo[x][y];
    }
}

原文地址:https://www.cnblogs.com/qinyuguan/p/11335447.html

时间: 2024-10-10 08:31:39

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

【python-leetcode329-深度优先搜索】矩阵中的最长递增路径

给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 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].注意不允许在对角线方向上移动. cl

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

题目要求: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1]] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9]. class Solution { public: int dx[5] = {-1, 0, 1, 0}; int dy[5] = {0, 1, 0, -1}; int longest

[LeetCode]Longest Increasing Path in a Matrix

题目: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 bounda

[LeetCode][JavaScript]Longest Increasing Path in a Matrix

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

[email&#160;protected] [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)

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 directions: left, right, up or down. You may NOT move diagonally o

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 the boundary (i.e. wrap-around is not all

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)

【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 four directions: left, right, up or down. You may NOT move dia

Leetcode之深度优先搜索&amp;回溯专题-679. 24 点游戏(24 Game)

深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: (8-4) * (7-1) = 24 示例 2: 输入: [1, 2, 1, 2] 输出: False 注意: 除法运算符 / 表示实数除法,而不是整数除法.例如 4 / (1 - 2/3) = 12 . 每个运算符对两个数进行运算.特别是我们不能用 - 作为一元运算符.例如,[1, 1