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

最长上升连续子序列 Linkcode

时间:2017-07-21 00:03:22      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:返回   bsp   log   问题:   turn   max   连续   整数   incr   

问题:

给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)

 

样例

给定 [5, 4, 2, 1, 3], 其最长上升连续子序列(LICS)为 [5, 4, 2, 1], 返回 4.

给定 [5, 1, 2, 3, 4], 其最长上升连续子序列(LICS)为 [1, 2, 3, 4], 返回 4.

给定 [1, 1, 1, 1, 1], 其最长上升连续子序列(LICS)为 [1], 返回 1.

 1 public class Solution {
 2     /**
 3      * @param A an array of Integer
 4      * @return  an integer
 5      */
 6     public static int longestIncreasingContinuousSubsequence(int[] A) {
 7         if(A.length==0)
 8             return 0;
 9         int hz,hj,r,maxz,maxj;
10         hz=hj=0;
11         r=maxz=maxj=1;
12         
13         while(r<A.length){
14             if(A[r]<A[r-1]){
15                 maxz = Math.max(maxz, r-hz);
16                 hz = r;
17             }else if(A[r]>A[r-1]){
18                 maxj = Math.max(maxj, r-hj);
19                 hj = r;
20             }else{
21                 hz = hj = r;
22             }
23             r++;
24         } 
25         maxz = Math.max(maxz, r-hz);
26         maxj = Math.max(maxj, r-hj);
27         return Math.max(maxz, maxj);
28     }
29 }

 

最长上升连续子序列 Linkcode

标签:返回   bsp   log   问题:   turn   max   连续   整数   incr   

原文地址:http://www.cnblogs.com/xfcnbkk/p/7215049.html

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