674. Longest Consecutive Increasing Sequence

Given an unsorted array of integers, find the longest and contiguous increasing sequence.

Example 1:

输入: [1,3,5,4,7]
输出: 3
解释: 最长连续递增序列是 [1,3,5], 长度为3。
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为5和7在原数组里被4隔开。 

Example 2:

输入: [2,2,2,2,2]
输出: 1
解释: 最长连续递增序列是 [2], 长度为1。

Note: The array length will not exceed 10000.

/**
 * Created by Joe on 2018/4/16.
 * 674. Longest Continuous Increasing Subsequence
 * https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/
 */
public class P674 {
    public int findLengthOfLCIS(int[] nums) {
        if (nums == null || nums.length == 0) return 0;

        int[] length = new int[nums.length];
        length[0] = 1;

        int maxLen = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i - 1]) {
                length[i] = length[i-1] + 1;
            } else {
                length[i] = 1;
            }

            maxLen = Math.max(maxLen, length[i]);
        }

        return maxLen;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324601980&siteId=291194637