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).
思路:
- 對陣列中每個元素A,都嘗試尋找以A為起點的最長路徑。
- 用一個最大值路徑變數記錄當前尋找到的最長路徑。
- 由於嘗試遍歷每個元素,陣列中某些位置會被重複查詢,此時可以通過雜湊表來記錄以當前位置為起點的最長路徑,如果已經查詢過,則直接返回值。如果沒有被查詢過,在算出最大路徑值以後存放到雜湊表中,下次就不用再計算了。
public class LIPInMatrix329 {
private static final int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
private int m, n;
public int longestIncreasingPath(int[][] matrix) {
if (matrix.length == 0) {
return 0;
}
m = matrix.length; n = matrix[0].length;
int[][] cache = new int[m][n];
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans = Math.max(ans, dfs(matrix, i, j, cache));
}
}
return ans;
}
private int dfs(int[][] matrix, int x, int y, int[][] cache) {
if (cache[x][y] != 0) {
return cache[x][y];
}
cache[x][y] = 1;
for (int[] d : dirs) {
int tx = x + d[0], ty = y + d[1];
if (0 <= tx && tx < m && 0 <= ty && ty < n && matrix[tx][ty] > matrix[x][y]) {
cache[x][y] = Math.max(cache[x][y], 1 + dfs(matrix, tx, ty, cache));
}
}
return cache[x][y];
}
}
相關文章
- Leetcode 329. Longest Increasing Path in a Matrix (python+cpp)LeetCodePython
- [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] 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筆記
- leetcode學習筆記73 Set Matrix ZeroesLeetCode筆記
- LeetCode Longest Substring Without Repeating Characters(003)解法總結LeetCode