//最长连续递增序列
/*
* 给定一个未经排序的整数数组,找到最长且连续递增的子序列,并返回该序列的长度
* */
public class P19 {
public static void main(String[] args) {
System.out.println(findLength(new int[]{1,2,3,2,3,4,3,4,5,6,7}));
System.out.println(findLength(new int[]{4,2,1}));
}
//贪心算法
public static int findLength(int[] nums){
int start = 0;
int max = 1;
for (int i=1; i<nums.length; i++){
if(nums[i] <= nums[i-1]){ //不是递增了,重置
start = i; //结果就是i-start+1就是最大长度
}
max = Math.max(max, i - start + 1);
}
return max;
}
}
网友评论