zoukankan      html  css  js  c++  java
  • LintCode "Longest Increasing Continuous subsequence II" !!

    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;
        }
    };
  • 相关阅读:
    TestNG详解-深度好文
    自动化测试用例getText()获取某一个元素的值返回null或空
    Python装饰器
    工资管理系统
    并发,并行
    TCP/IP协议
    tcp长连接和短连接
    tcp注意点
    文件下载器
    tcp服务器
  • 原文地址:https://www.cnblogs.com/tonix/p/4967952.html
Copyright © 2011-2022 走看看