编程语言
首页 > 编程语言> > P19-最长连续递增序列-贪心算法

P19-最长连续递增序列-贪心算法

作者:互联网

//最长连续递增序列
/*
* 给定一个未经排序的整数数组,找到最长且连续递增的子序列,并返回该序列的长度
* */
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;
    }
}

 

标签:int,递增,findLength,System,算法,序列,P19,public,贪心
来源: https://www.cnblogs.com/YonchanLew/p/16129190.html