July 30, 2023
정수 배열의 숫자가 주어지면, 엄격하게 증가하는 가장 긴 길이의 서브 시퀀스.
Input: nums = [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.Input: nums = [0,1,0,3,2,3]
Output: 4Input: nums = [7,7,7,7,7,7,7]
Output: 1제약 조건:
1 <= nums.length <= 2500-104 <= nums[i] <= 104동적 계획법을 이용하여 접근합니다. 배열의 두번째 자료부터 시작하여 이전 요소와 비교합니다.
function soultion(nums) {
const temp = new Array(nums.length).fill(1);
for (let i = 1; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
temp[i] = Math.max(temp[i], temp[j] + 1);
}
}
}
return Math.max(...temp);
};