Skip to content

Commit e1c705d

Browse files
committed
solve: longest increasing subsequence
1 parent 9f452a9 commit e1c705d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* TC: O(N^2)
3+
* SC: O(N)
4+
* N: nums.length
5+
*/
6+
7+
/**
8+
* @param {number[]} nums
9+
* @return {number}
10+
*/
11+
var lengthOfLIS = function (nums) {
12+
// 각자 스스로는 최소 1의 lengthOfLIS를 가짐
13+
const longestLength = new Array(nums.length).fill(1);
14+
15+
// nums배열의 right까지 원소들 중 lengthOfLIS를 저장
16+
for (let right = 1; right < nums.length; right++) {
17+
for (let left = 0; left < right; left++) {
18+
if (nums[left] < nums[right]) {
19+
longestLength[right] = Math.max(
20+
longestLength[right],
21+
longestLength[left] + 1
22+
);
23+
}
24+
}
25+
}
26+
27+
return Math.max(...longestLength);
28+
};

0 commit comments

Comments
 (0)