【Lintcode】398. Longest Continuous Increasing Subsequence II
題目地址:
https://www.lintcode.com/problem/longest-continuous-increasing-subsequence-ii/description
給定一個二維矩陣 A A A,從某個位置出發,可以向上下左右走一步。問由嚴格上升數字構成的最長路徑的長度。
思路是記憶化搜尋。設 f [ i ] [ j ] f[i][j] f[i][j]是從 A [ i ] [ j ] A[i][j] A[i][j]出發的最長上升路徑的長度,則 f [ i ] [ j ] f[i][j] f[i][j]可以由 1 1 1加上四周比 A [ i ] [ j ] A[i][j] A[i][j]大的數出發的最長上升路徑的長度來更新。如果不存在,那麼 f [ i ] [ j ] = 1 f[i][j]=1 f[i][j]=1。程式碼如下:
public class Solution {
/**
* @param matrix: A 2D-array of integers
* @return: an integer
*/
public int longestContinuousIncreasingSubsequence2(int[][] matrix) {
// write your code here
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int m = matrix.length, n = matrix[0].length;
int[][] dp = new int[m][n];
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
res = Math.max(res, dfs(i, j, dp, matrix));
}
}
return res;
}
private int dfs(int x, int y, int[][] dp, int[][] matrix) {
// 有記憶,則調取記憶
if (dp[x][y] != 0) {
return dp[x][y];
}
// 先初始化為1
dp[x][y] = 1;
int[] d = {1, 0, -1, 0, 1};
for (int i = 0; i < 4; i++) {
int nextX = x + d[i], nextY = y + d[i + 1];
// 如果沒出界,並且可以接上去,就繼續DFS
if (0 <= nextX && nextX < matrix.length && 0 <= nextY && nextY < matrix[0].length && matrix[nextX][nextY] > matrix[x][y]) {
dp[x][y] = Math.max(dp[x][y], 1 + dfs(nextX, nextY, dp, matrix));
}
}
return dp[x][y];
}
}
時空複雜度 O ( m n ) O(mn) O(mn)。
相關文章
- [LeetCode] 674. Longest Continuous Increasing SubsequenceLeetCode
- 673. Number of Longest Increasing Subsequence
- Lintcode 1263. Is Subsequence
- Leetcode 329. Longest Increasing Path in a MatrixLeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- 最長公共子序列 Longest Common Subsequence
- Leetcode 329. Longest Increasing Path in a Matrix (python+cpp)LeetCodePython
- 300-Longest Increasing Subsequnce-最長遞增子序列
- Increasing Sequence with Fixed OR
- Missing Subsequence Sum
- leetcode392. Is SubsequenceLeetCode
- [LeetCode] 523. Continuous Subarray SumLeetCode
- Longest Valid Parentheses
- Longest Univalue Path
- CF1922E Increasing Subsequences
- E. Increasing Subsequences__2
- 領釦LintCode演算法問題答案-1354. 楊輝三角形II演算法
- [atcoder 349] [F - Subsequence LCM]
- B. Missing Subsequence Sum
- LeetCode之Reveal Cards In Increasing Order(Kotlin)LeetCodeKotlin
- LeetCode之Increasing Order Search Tree(Kotlin)LeetCodeKotlin
- [LeetCode] 727. Minimum Window SubsequenceLeetCode
- Leetcode 32 Longest Valid ParenthesesLeetCode
- Leetcode 14 Longest Common PrefixLeetCode
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- 687-Longest Univalue Path
- [LintCode] Daily TemperaturesAI
- [LintCode] Permutation in String
- [ABC292G] Count Strictly Increasing Sequences
- CF163A Substring and Subsequence 題解
- 簡單dp -- Common Subsequence POJ - 1458
- CF1580D Subsequence 題解
- 【Leetcode】1081. Smallest Subsequence of Distinct CharactersLeetCode
- 【Leetcode】1673. Find the Most Competitive SubsequenceLeetCode
- [LeetCode] 32. Longest Valid ParenthesesLeetCode
- [LeetCode] 5. Longest Palindromic SLeetCode
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- 【Lintcode】1189. Minesweeper