1.문제
Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.
A continuous increasing subsequence is defined by two indices l and r (l < r) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] and for each l <= i < r, nums[i] < nums[i + 1].
정수 배열이 주어질 때 정수 배열의 부부 배열중 연속으로 증가하는 부분 배열의 최대 길이값을 리턴하는 문제이다.
Example 1
Input: nums = [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3.
Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element
4.
Example 2
Input: nums = [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly
increasing.
Constraints:
- 1 <= nums.length <= 10^4
- -10^9 <= nums[i] <= 10^9
2.풀이
- 배열을 순회하면서 현재의 길이를 기록해둔다.
- 만약 배열 값이 감소하는 순간이 오면 현재까지 기록한 길이와 최대길이를 비교해서 큰 값을 최대길이 값으로 갱신한다.
- for문이 끝나면 현재길이값과 최대길이값중 큰 값을 리턴해준다.
/**
* @param {number[]} nums
* @return {number}
*/
const findLengthOfLCIS = function (nums) {
let maxLength = 0;
let current = 0;
for (let i = 0; i < nums.length; i++) {
// 배열을 순회하면서
current++; // 현재 길이 1 증가
if (nums[i + 1] <= nums[i]) {
// 만약 감소하는 부분이 나타나면
if (current > maxLength) {
// 현재 길이와 최대길이 중 큰 값을 최대길이로 갱신
maxLength = current;
}
current = 0; // 현재 길이 0 으로 초기화
}
}
return Math.max(current, maxLength); // 최대 길이와 현재 길이중 최대값을 리턴한다.
};
3.결과
