最长上升连续子序列

来源:互联网 发布:java运行环境官网下载 编辑:程序博客网 时间:2024/05/20 08:24
给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)
 注意事项
time
样例
给定 [5, 4, 2, 1, 3], 其最长上升连续子序列(LICS)为 [5, 4, 2, 1], 返回 4.

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


import java.util.Scanner;/** * 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。) 注意事项time样例给定 [5, 4, 2, 1, 3], 其最长上升连续子序列(LICS)为 [5, 4, 2, 1], 返回 4.给定 [5, 1, 2, 3, 4], 其最长上升连续子序列(LICS)为 [1, 2, 3, 4], 返回 4. *  * @author Dell * */public class Test397 {  public static int longestIncreasingContinuousSubsequence(int[] A)  {    if(A.length==0)    return 0;  int maxLength=1;  int maxlength1=1;  int count=1;  int count1=1;  for(int i=0;i<A.length-1;i++)  {  if(A[i+1]<A[i])  {  count++;  }  else  {  if(count>maxLength)  {  maxLength=count;  }  count=1;  }   if(A[i+1]>A[i])  {  count1++;  }    else  {  if(count1>maxLength)  {  maxLength=count1;  }  count1=1;  }     }  if(count>maxLength)  {  maxLength=count;  }  if(count1>maxLength)  {  maxLength=count1;  } return maxLength;   }public static void main(String[] args) {Scanner sc=new Scanner(System.in);int n=sc.nextInt();int[] a=new int[n];for(int i=0;i<a.length;i++){a[i]=sc.nextInt();}     System.out.println(longestIncreasingContinuousSubsequence(a));}}