标签:
DFS + Memorized Search (DP)
class Solution { int dfs(int i, int j, int row, int col, vector<vector<int>>& A, vector<vector<int>>& dp) { if(dp[i][j] != 0) return dp[i][j]; if (i > 0 && A[i-1][j] > A[i][j]) { dp[i][j] = max(dp[i][j], dfs(i - 1, j, row, col, A, dp)); } if (i < row - 1 && A[i+1][j] > A[i][j]) { dp[i][j] = max(dp[i][j], dfs(i + 1, j, row, col, A, dp)); } if (j > 0 && A[i][j-1] > A[i][j]) { dp[i][j] = max(dp[i][j], dfs(i, j - 1, row, col, A, dp)); } if (j < col - 1 && A[i][j+1] > A[i][j]) { dp[i][j] = max(dp[i][j], dfs(i, j + 1, row, col, A, dp)); } return ++dp[i][j]; } public: /** * @param A an integer matrix * @return an integer */ int longestIncreasingContinuousSubsequenceII(vector<vector<int>>& A) { if (A.empty() || A[0].empty()) return 0; int ret = 0; int row = A.size(); int col = A[0].size(); vector<vector<int>> dp(row, vector<int>(col)); for(int i = 0; i < row; i ++) for(int j = 0; j < col; j ++) { ret = max(ret, dfs(i, j, row, col, A, dp)); } return ret; } };
LintCode "Longest Increasing Continuous subsequence II" !!
标签:
原文地址:http://www.cnblogs.com/tonix/p/4967952.html