Leetcode 329. Longest Increasing Path in a Matrix (python+cpp)
題目
解法1:dfs暴力
class Solution:
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
def dfs(i,j,prev):
if i<0 or i>=m or j<0 or j>=n or matrix[i][j]<=prev:
return 0
#print(i,j)
tmp = 0
tmp = max(dfs(i+1,j,matrix[i][j]),dfs(i,j+1,matrix[i][j]),dfs(i-1,j,matrix[i][j]),dfs(i,j-1,matrix[i][j])) + 1
return tmp
if not matrix or not matrix[0]:
return 0
m,n = len(matrix),len(matrix[0])
#dfs(0,1,float('-inf'))
ans = 0
for i in range(m):
for j in range(n):
print((i,j),dfs(i,j,float('-inf')))
ans = max(dfs(i,j,float('-inf')),ans)
return ans
解法2:dfs+memorization
class Solution:
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
def dfs(i,j,prev):
if i<0 or i>=m or j<0 or j>=n or matrix[i][j]<=prev:
return 0
if memo[i][j] != float('-inf'):
return memo[i][j]
memo[i][j] = max(dfs(i+1,j,matrix[i][j]),dfs(i,j+1,matrix[i][j]),dfs(i-1,j,matrix[i][j]),dfs(i,j-1,matrix[i][j])) + 1
return memo[i][j]
if not matrix or not matrix[0]:
return 0
m,n = len(matrix),len(matrix[0])
ans = 0
memo = [[float('-inf')]*n for _ in range(m)]
for i in range(m):
for j in range(n):
ans = max(dfs(i,j,float('-inf')),ans)
return ans
C++版本:
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[0].empty()) return 0;
int ans = 0;
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> memo(m,vector<int>(n,-INT_MAX));
for (int i=0;i<matrix.size();i++){
for (int j=0;j<matrix[0].size();j++){
ans = max(ans,dfs(i,j,-INT_MAX,memo,matrix));
}
}
return ans;
}
int dfs(int i, int j, int prev, vector<vector<int>>& memo, vector<vector<int>>& matrix){
if (i<0 || i>=matrix.size() || j<0 || j>=matrix[0].size() || matrix[i][j]<=prev) {
return 0;
}
if (memo[i][j] != -INT_MAX) return memo[i][j];
memo[i][j] = max(dfs(i+1,j,matrix[i][j],memo,matrix)+1,memo[i][j]);
memo[i][j] = max(dfs(i,j+1,matrix[i][j],memo,matrix)+1,memo[i][j]);
memo[i][j] = max(dfs(i-1,j,matrix[i][j],memo,matrix)+1,memo[i][j]);
memo[i][j] = max(dfs(i,j-1,matrix[i][j],memo,matrix)+1,memo[i][j]);
return memo[i][j];
}
};
相關文章
- Leetcode 329. Longest Increasing Path in a MatrixLeetCode
- [LeetCode] 674. Longest Continuous Increasing SubsequenceLeetCode
- leetcode388. Longest Absolute File PathLeetCode
- 673. Number of Longest Increasing Subsequence
- Longest Univalue Path
- 【Lintcode】398. Longest Continuous Increasing Subsequence II
- 687-Longest Univalue Path
- 300-Longest Increasing Subsequnce-最長遞增子序列
- Leetcode 54 Spiral MatrixLeetCode
- Leetcode 32 Longest Valid ParenthesesLeetCode
- Leetcode 14 Longest Common PrefixLeetCode
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- LeetCode之Reveal Cards In Increasing Order(Kotlin)LeetCodeKotlin
- LeetCode之Increasing Order Search Tree(Kotlin)LeetCodeKotlin
- Leetcode 1366. Rank Teams by Votes (python+cpp)LeetCodePython
- Leetcode 1375. Bulb Switcher III (python+cpp)LeetCodePython
- [LeetCode] 867. Transpose MatrixLeetCode
- LeetCode 542. 01 MatrixLeetCode
- [LeetCode] 32. Longest Valid ParenthesesLeetCode
- [LeetCode] 5. Longest Palindromic SLeetCode
- Leetcode Path SumLeetCode
- Leetcode 73. Set Matrix ZeroesLeetCode
- [LeetCode] 2326. Spiral Matrix IVLeetCode
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- Leetcode 298 Binary Tree Longest Consecutive SequenceLeetCode
- [LeetCode] 2831. Find the Longest Equal SubarrayLeetCode
- [LeetCode] 2419. Longest Subarray With Maximum Bitwise ANDLeetCode
- Leetcode 71 Simplify PathLeetCode
- [LeetCode] 524. Longest Word in Dictionary through DeletingLeetCode
- LeetCode Longest Common Prefix(014)解法總結LeetCode
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- LeetCode 71. Simplify PathLeetCode
- LeetCode 112. Path SumLeetCode
- [LeetCode] 378. Kth Smallest Element in a Sorted MatrixLeetCode
- C# 寫 LeetCode easy #14 Longest Common PrefixC#LeetCode
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript
- leetcode學習筆記14 Longest Common PrefixLeetCode筆記