LeetCode 329. Longest Increasing Path in a Matrix

栏目: 编程工具 · 发布时间: 6年前

内容简介: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

Description

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

Example 2:

Input: nums = 
[
  [3,4,5],
  [3,2,6],
  [2,2,1]
] 
Output: 4 
Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.

描述

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

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

示例 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]。注意不允许在对角线方向上移动。

思路

  • 这道题主要使用记忆化递归和深度优先遍历。
  • 我们以给定的矩阵的每一个位置为起点,进行深度优先遍历。
  • 我们存储每个位置深度优先遍历的结果,当下一次走到这个位置的时候,我们直接返回当前位置记录的值,这样可以减少遍历的次数,加快执行速度。
  • 二维矩阵 dp 初始化每个位置都为 0 ,当遍历到某个位置不为 0 的时候,说明该位置已经遍历过了,我们直接返回其值。
# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-03-07 21:19:51
# @Last Modified by:   何睿
# @Last Modified time: 2019-03-07 22:23:10

from itertools import product


class Solution:
    def longestIncreasingPath(self, matrix: [[int]]) -> int:
        # 如果矩阵为空,返回 0
        if not matrix or not matrix[0]: return 0
        # 获取矩阵的行数和列数
        row, col = len(matrix), len(matrix[0])
        # 记忆化递归,记录每个位置的最大值
        dp = [[0] * col for _ in range(row)]
        # 遍历每一个位置,以每一个位置为起点进行深度优先遍历
        # 返回最大值
        return max(
            self._dfs(i, j, row, col, matrix, dp)
            for i, j in product(range(row), range(col)))

    def _dfs(self, i, j, row, col, matrix, dp):
        # 如果当前位置不为零,说明当前位置的最大值已经被找到
        # 采用记忆化递归,直接返回最大值
        if dp[i][j]: return dp[i][j]
        # 遍历四个方向
        for x, y in [(0, -1), (-1, 0), (0, 1), (1, 0)]:
            m, n = x + i, y + j
            # 如果下一个位置没有越界并且下一个位置的只严格大于位置i,j
            if 0 <= m < row and 0 <= n < col and matrix[i][j] < matrix[m][n]:
                # 记录最大值
                dp[i][j] = max(dp[i][j], self._dfs(m, n, row, col, matrix, dp))
        # 把当前位置本身加上
        dp[i][j] += 1
        # 返回以当前位置为起点,所有路径中的最大值
        return dp[i][j]

源代码文件在 这里

©本文首发于 何睿的博客 ,欢迎转载,转载需保留 文章来源 ,作者信息和本声明.

微信公众号:techruicore


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Practical JavaScript, DOM Scripting and Ajax Projects

Practical JavaScript, DOM Scripting and Ajax Projects

Frank Zammetti / Apress / April 16, 2007 / $44.99

http://www.amazon.com/exec/obidos/tg/detail/-/1590598164/ Book Description Practical JavaScript, DOM, and Ajax Projects is ideal for web developers already experienced in JavaScript who want to ......一起来看看 《Practical JavaScript, DOM Scripting and Ajax Projects》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具