Home LeetCode - 674. Longest Continuous Increasing Subsequence
Post
Cancel

LeetCode - 674. Longest Continuous Increasing Subsequence

674. Longest Continuous Increasing Subsequence - easy

문제

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).

제한사항

  • Length of the array will not exceed 10,000.

입출력 예

1
2
3
4
5
6
Example 1:

Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. 
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.
1
2
3
4
5
Example 2:

Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1.

풀이

  • Array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        if (nums.empty()) {
            return 0;
        }
        
        int maxLength = 0;
        
        int count = 1;
        for (auto i = 0 ; i < nums.size() - 1; ++i) {
            if (nums[i] < nums[i+1]) {
                ++count;
            } else {
                maxLength = std::max(count, maxLength);
                count = 1;
            }
        }
        
        maxLength = std::max(count, maxLength);

        return maxLength;
    }
};
This post is licensed under CC BY 4.0 by the author.