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

[LintCode] Longest Increasing Continuous subsequence

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