LintCode "Longest Increasing Continuous subsequence II" !!

时间:2021-03-23 16:01:28

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] != ) return dp[i][j]; if (i > && A[i-][j] > A[i][j])
{
dp[i][j] = max(dp[i][j], dfs(i - , j, row, col, A, dp));
}
if (i < row - && A[i+][j] > A[i][j])
{
dp[i][j] = max(dp[i][j], dfs(i + , j, row, col, A, dp));
}
if (j > && A[i][j-] > A[i][j])
{
dp[i][j] = max(dp[i][j], dfs(i, j - , row, col, A, dp));
}
if (j < col - && A[i][j+] > A[i][j])
{
dp[i][j] = max(dp[i][j], dfs(i, j + , 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[].empty()) return ; int ret = ;
int row = A.size();
int col = A[].size(); vector<vector<int>> dp(row, vector<int>(col)); for(int i = ; i < row; i ++)
for(int j = ; j < col; j ++)
{
ret = max(ret, dfs(i, j, row, col, A, dp));
} return ret;
}
};