首页 > 其他 > 详细

[lintcode easy]Longest Increasing Continuous subsequence

时间:2015-12-01 07:08:30      阅读:232      评论:0      收藏:0      [点我收藏+]

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.

Note

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.length==0) return 0;
        int count=2;
        int maxCount=1;
        
        
        for(int i=1;i<A.length-1;i++)
        {
            if(A[i]>A[i+1] && A[i-1]>A[i] )
            {
                count++;
                maxCount=Math.max(maxCount,count);
                
            }
            else if(A[i]<A[i+1]&&A[i-1]<A[i])
            {
                count++;
                maxCount=Math.max(maxCount,count);
            }
            else
            {
                count=2;
            }
            
            
        }
        
        return maxCount;
    }
}

 

[lintcode easy]Longest Increasing Continuous subsequence

原文:http://www.cnblogs.com/kittyamin/p/5008997.html

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