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.
난이도: easy
정렬되지 않은 리스트가 있다
오름차순으로 증가하는 리스트의 길이 중 가장 긴 길이를 구해라
처음엔 문제를 잘못이해 하여 일정한 간격으로 증가하는 리스트의 길이를 구하는 줄 알았다
일정하지 않더라도 증가만 하면 되는 조건으로 구해주면 된다
String, value
var findLengthOfLCIS = function(nums) {
let maxLength = 0;
let currentLength = 0;
for (let i = 0; i < nums.length; i++) {
if ((i === 0) || nums[i] > nums[i - 1]) {
currentLength += 1
} else {
currentLength = 1
}
if (currentLength > maxLength) {
maxLength = currentLength
}
}
return maxLength;
};
1번의 예제를 보면
[1, 3, 5, 4, 7]
[1, 3, 5]과 [4, 7] 두가지로 나뉘어 진다
각각 오름차순으로 증가를 하고 있지만, 둘의 길이를 비교했을 때 가장 긴 길이는 3이다
반응형
'Algorithm' 카테고리의 다른 글
[Leetcode] 1160. Find Words That Can Be Formed by Characters (0) | 2022.03.06 |
---|---|
[Leetcode] 605. Can Place Flowers (0) | 2022.03.05 |
[Leetcode] 155. Min Stack (0) | 2022.03.05 |
[Leetcode] 1306. Jump Game III (0) | 2022.03.04 |
[Leetcode] 860. Lemonade Change (0) | 2022.03.04 |