Skip to content

Commit bd021c9

Browse files
committed
longest-increasing-subsequence
1 parent ef86fcc commit bd021c9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var lengthOfLIS = function (nums) {
6+
let sub = [nums[0]]; // 가장 첫번째 원소 일단 넣고 보기
7+
for (const num of nums.slice(1)) {
8+
if (num > sub[sub.length - 1]) {
9+
// 수열의 마지막값보다 크면 무조건 추가
10+
sub.push(num);
11+
} else {
12+
// 수열의 마지막값보다 작다면 이 숫자보다 큰 숫자들 중 가장 작은 숫자를 찾아서 교체
13+
let i = 0;
14+
while (sub[i] < num) {
15+
i++;
16+
}
17+
sub[i] = num;
18+
}
19+
}
20+
return sub.length;
21+
};

0 commit comments

Comments
 (0)