Skip to content

Commit 1c75461

Browse files
committed
longest-increasing-subsequence solution
1 parent 0060e06 commit 1c75461

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n log n), Space: O(n)
2+
function lengthOfLIS(nums) {
3+
const tails = [];
4+
5+
for (const num of nums) {
6+
let left = 0, right = tails.length;
7+
8+
// 이진 탐색: tails에서 num이 들어갈 최소 위치 찾기
9+
while (left < right) {
10+
const mid = Math.floor((left + right) / 2);
11+
if (tails[mid] < num) {
12+
left = mid + 1;
13+
} else {
14+
right = mid;
15+
}
16+
}
17+
18+
// left는 삽입 위치
19+
tails[left] = num;
20+
}
21+
22+
return tails.length;
23+
}

0 commit comments

Comments
 (0)