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

lintcode-easy-Longest Increasing Continuous Subsequence

时间:2016-02-26 08:07:58      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

Give an integer array,find the longest increasing continuous subsequence in this array.

An increasing continuous subsequence:

  • Can be from right to left or from left to right.
  • Indices of the integers in the subsequence should be continuous.
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.

Notice

O(n) time and O(1) extra space.

 

public class Solution {
    /**
     * @param A an array of Integer
     * @return  an integer
     */
    public int longestIncreasingContinuousSubsequence(int[] A) {
        // Write your code here
        if(A == null || A.length == 0)
            return 0;
        
        if(A.length == 1)
            return 1;
        
        int result = 1;
        int count = 1;
        
        for(int i = 1; i < A.length; i++){
            if(A[i] > A[i - 1])
                count++;
            else
                count = 1;
            if(count > result)
                result = count;
        }
        
        count = 1;
        
        for(int i = A.length - 2; i >= 0; i--){
            if(A[i] > A[i + 1])
                count++;
            else
                count = 1;
            if(count > result)
                result = count;
        }
        
        return result;
    }
}

 

lintcode-easy-Longest Increasing Continuous Subsequence

标签:

原文地址:http://www.cnblogs.com/goblinengineer/p/5218944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!