首页 > 其他 > 详细

LintCode "Longest Increasing Continuous subsequence II" !!

时间:2015-11-16 06:08:18      阅读:241      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!