码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode "Longest Increasing Continuous subsequence II" !!

时间:2015-11-16 06:09:19      阅读:287      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!