首页 > 其他 > 详细

[LintCode] Longest Increasing Continuous subsequence

时间:2015-05-26 12:16:58      阅读:647      评论:0      收藏:0      [点我收藏+]

http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/#

Give you an integer array (index from 0 to n-1, where n is the size of this array),find the longest increasing continuous subsequence in this array. (The definition of the longest increasing continuous subsequence here can be from right to left or from left to right)

Example

For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.

For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.

基础的DP问题,直接上代码:

class Solution {
public:
    /**
     * @param A an array of Integer
     * @return  an integer
     */
    int longestIncreasingContinuousSubsequence(vector<int>& A) {
        if (A.empty()) {
            return 0;
        }
        
        int *state = new int[A.size()]();
        
        state[0] = 1;
        for (int ix = 1; ix < A.size(); ix++) {
            if (A[ix] > A[ix - 1]) {
                state[ix] = state[ix - 1] + 1;
            } else {
                state[ix] = 1;
            }
        }
        int leftToRight = *max_element(state, state + A.size());
        
  
        state[0] = 1;
        for (int ix = 1; ix < A.size(); ix++) {
            if (A[ix] < A[ix - 1]) {
                state[ix] = state[ix - 1] + 1;
            } else {
                state[ix] = 1;
            }
        }
        int rightToLeft = *max_element(state, state + A.size());
        
        return max(leftToRight, rightToLeft);
    }
};

[LintCode] Longest Increasing Continuous subsequence

原文:http://www.cnblogs.com/jianxinzhou/p/4530257.html

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