摘要:思路這道題主要使用記憶化遞歸和深度優(yōu)先遍歷。我們以給定的矩陣的每一個(gè)位置為起點(diǎn),進(jìn)行深度優(yōu)先遍歷。我們存儲(chǔ)每個(gè)位置深度優(yōu)先遍歷的結(jié)果,當(dāng)下一次走到這個(gè)位置的時(shí)候,我們直接返回當(dāng)前位置記錄的值,這樣可以減少遍歷的次數(shù),加快執(zhí)行速度。
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.描述
給定一個(gè)整數(shù)矩陣,找出最長(zhǎng)遞增路徑的長(zhǎng)度。
對(duì)于每個(gè)單元格,你可以往上,下,左,右四個(gè)方向移動(dòng)。 你不能在對(duì)角線方向上移動(dòng)或移動(dòng)到邊界外(即不允許環(huán)繞)。
示例 1:
輸入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 輸出: 4 解釋: 最長(zhǎng)遞增路徑為 [1, 2, 6, 9]。
示例 2:
輸入: nums = [ [3,4,5], [3,2,6], [2,2,1] ] 輸出: 4 解釋: 最長(zhǎng)遞增路徑是 [3, 4, 5, 6]。注意不允許在對(duì)角線方向上移動(dòng)。思路
這道題主要使用記憶化遞歸和深度優(yōu)先遍歷。
我們以給定的矩陣的每一個(gè)位置為起點(diǎn),進(jìn)行深度優(yōu)先遍歷。
我們存儲(chǔ)每個(gè)位置深度優(yōu)先遍歷的結(jié)果,當(dāng)下一次走到這個(gè)位置的時(shí)候,我們直接返回當(dāng)前位置記錄的值,這樣可以減少遍歷的次數(shù),加快執(zhí)行速度。
二維矩陣 dp 初始化每個(gè)位置都為 0 ,當(dāng)遍歷到某個(gè)位置不為 0 的時(shí)候,說(shuō)明該位置已經(jīng)遍歷過(guò)了,我們直接返回其值。
# -*- 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 # 獲取矩陣的行數(shù)和列數(shù) row, col = len(matrix), len(matrix[0]) # 記憶化遞歸,記錄每個(gè)位置的最大值 dp = [[0] * col for _ in range(row)] # 遍歷每一個(gè)位置,以每一個(gè)位置為起點(diǎn)進(jìn)行深度優(yōu)先遍歷 # 返回最大值 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): # 如果當(dāng)前位置不為零,說(shuō)明當(dāng)前位置的最大值已經(jīng)被找到 # 采用記憶化遞歸,直接返回最大值 if dp[i][j]: return dp[i][j] # 遍歷四個(gè)方向 for x, y in [(0, -1), (-1, 0), (0, 1), (1, 0)]: m, n = x + i, y + j # 如果下一個(gè)位置沒(méi)有越界并且下一個(gè)位置的只嚴(yán)格大于位置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)) # 把當(dāng)前位置本身加上 dp[i][j] += 1 # 返回以當(dāng)前位置為起點(diǎn),所有路徑中的最大值 return dp[i][j]
源代碼文件在 這里 。
?本文首發(fā)于 何睿的博客 ,歡迎轉(zhuǎn)載,轉(zhuǎn)載需保留 文章來(lái)源 ,作者信息和本聲明.
微信公眾號(hào):techruicore
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/43302.html
摘要:題目要求思路和代碼這里采用廣度優(yōu)先算法加上緩存的方式來(lái)實(shí)現(xiàn)。我們可以看到,以一個(gè)節(jié)點(diǎn)作為開(kāi)始構(gòu)成的最長(zhǎng)路徑長(zhǎng)度是確定的。因此我們可以充分利用之前得到的結(jié)論來(lái)減少重復(fù)遍歷的次數(shù)。 題目要求 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...
摘要:復(fù)雜度思路為了避免搜索已經(jīng)搜索的點(diǎn)。所以考慮用一個(gè)數(shù)組,記錄到每一個(gè)點(diǎn)能產(chǎn)生的最長(zhǎng)序列的長(zhǎng)度。考慮用進(jìn)行搜索,對(duì)于每一個(gè)點(diǎn)來(lái)說(shuō),考慮先找到最小的那個(gè)點(diǎn)針對(duì)每一條路徑的。然后對(duì)于每一點(diǎn)再遞增回去,依次累積找到增加的值。 LeetCode[329] Longest Increasing Path in a Matrix Given an integer matrix, find the ...
Problem 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 ou...
Problem 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 ou...
摘要:題目解答最重要的是用保存每個(gè)掃過(guò)結(jié)點(diǎn)的最大路徑。我開(kāi)始做的時(shí)候,用全局變量記錄的沒(méi)有返回值,這樣很容易出錯(cuò),因?yàn)槿魏我粋€(gè)用到的環(huán)節(jié)都有可能改變值,所以還是在函數(shù)中定義,把當(dāng)前的直接返回計(jì)算不容易出錯(cuò)。 題目:Given an integer matrix, find the length of the longest increasing path. From each cell, y...
閱讀 3444·2021-11-25 09:43
閱讀 3491·2021-11-19 09:40
閱讀 2496·2021-10-14 09:48
閱讀 1337·2021-09-09 11:39
閱讀 1956·2019-08-30 15:54
閱讀 2848·2019-08-30 15:44
閱讀 2026·2019-08-29 13:12
閱讀 1570·2019-08-29 12:59