LeetCode 329 矩阵中的最长递增路径 题解

LeetCode 329 矩阵中的最长递增路径 题解

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

方法1:记忆化深度优先搜索

时间复杂度O(mn),其中 m 和 n 分别是矩阵的行数和列数。深度优先搜索的时间复杂度是 O(V+E),其中 V 是节点数,E 是边数。在矩阵中,O(V)=O(mn),O(E)≈O(4mn)=O(mn)。
空间复杂度O(mn),其中 m 和 n 分别是矩阵的行数和列数。空间复杂度主要取决于缓存和递归调用深度,缓存的空间复杂度是 O(mn),递归调用深度不会超过 mn

方法2:拓扑排序:将矩阵看成无权有向图

时间复杂度O(mn),其中 m 和 n 分别是矩阵的行数和列数。拓扑排序的时间复杂度是 O(V+E),其中 V 是节点数,E 是边数。在矩阵中,O(V)=O(mn)O(E)≈O(4mn)=O(mn)
空间复杂度O(mn),其中 m 和 n 分别是矩阵的行数和列数。空间复杂度主要取决于队列,队列中的元素个数不会超过 mn
在这里插入图片描述

import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author LiMin
 * @Title: LongestIncreasingPath
 * @Description: 给定一个整数矩阵,找出最长递增路径的长度。
 * @date 2020/9/10  11:13
 */
public class LongestIncreasingPath {
    public static Scanner in = new Scanner(new BufferedInputStream(System.in));
    public int rows, columns;
    public int[][] dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

    public int longestIncreasingPath1(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        rows = matrix.length;
        columns = matrix[0].length;
        int[][] memo = new int[rows][columns];
        int ans = 0;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < columns; ++j) {
                //这句写的很好
                ans = Math.max(ans, dfs1(matrix, i, j, memo));
            }
        }
        return ans;
    }

    //该函数代表以(row,column)为起点的最长递增路径的长度
    public int dfs1(int[][] matrix, int row, int column, int[][] memo) {
        //不用vis数组,直接根据距离数组判断有没有被访问过【被访问过就说明这个点
        //已经被计算过了,直接返回数值即可】
        if (memo[row][column] != 0) {
            return memo[row][column];
        }
        ++memo[row][column];//这句很有灵性,既然到了这里就说明这个点还没被计算过
        //,就给它赋初值1,所以这句话也可以换成memo[row][column]=1
        for (int[] dir : dirs) {
            int newRow = row + dir[0], newColumn = column + dir[1];
            if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns 
            && matrix[newRow][newColumn] > matrix[row][column]) {
                //这句话我本来担心去计算某一个点的过程中就把路径上的点给赋值了,
                //但是这个值又不是最终的值,那么万一其他点又遍历到这个点时岂不是
                //会返回一个错误的值
                //但其实不会,因为只会访问到比自己大的数,反过来不会访问到,所以
                //不用担心会访问到还没有赋最终距离值的点;并且在递归回归的时候会
                //把路径上经过的点的距离值一步一步更新
                memo[row][column] = Math.max(memo[row][column], 
                dfs1(matrix, newRow, newColumn, memo) + 1);
            }
        }
        return memo[row][column];
    }

    public int longestIncreasingPath2(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        rows = matrix.length;
        columns = matrix[0].length;
        int[][] outDegrees = new int[rows][columns];
        Queue<int[]> queue = new LinkedList<>();
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < columns; ++j) {
                //初始化每个点的出度
                for (int[] dir : dirs) {
                    int newRow = i + dir[0], newColumn = j + dir[1];
                    if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns 
                    && matrix[newRow][newColumn] > matrix[i][j]) {
                            ++outDegrees[i][j];
                    }
                }
                //出度为零的点入队
                if (outDegrees[i][j] == 0) {
                    queue.offer(new int[]{i, j});
                }

            }
        }
        int ans = 0;
        while (!queue.isEmpty()) {
            ++ans;//每一层相当于路径加一
            int size = queue.size();
            for (int i = 0; i < size; ++i) {
                int[] cur = queue.poll();
                int row = cur[0], col = cur[1];
                for (int[] dir : dirs) {
                    int newRow = row + dir[0], newColumn = col + dir[1];
                    if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && 
                    matrix[newRow][newColumn] < matrix[row][col]) {
                        //出度减一
                        --outDegrees[newRow][newColumn];
                        //出度为零的点入队
                        if (outDegrees[newRow][newColumn] == 0) {
                            queue.offer(new int[]{newRow, newColumn});
                        }
                    }
                }
            }
        }
        return ans;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值