Skip to content

Commit 2190278

Browse files
committed
Feat: 300. Longest Increasing Subsequence
1 parent a48ad19 commit 2190278

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* https://leetcode.com/problems/longest-increasing-subsequence
3+
* T.C. O(nlogn)
4+
* S.C. O(n)
5+
*/
6+
function lengthOfLIS(nums: number[]): number {
7+
const sub: number[] = [];
8+
9+
function findSlot(num: number): number {
10+
let left = 0;
11+
let right = sub.length - 1;
12+
13+
while (left <= right) {
14+
const mid = Math.floor((left + right) / 2);
15+
if (sub[mid] < num) {
16+
left = mid + 1;
17+
} else {
18+
right = mid - 1;
19+
}
20+
}
21+
22+
return left;
23+
}
24+
25+
for (let i = 0; i < nums.length; i++) {
26+
const num = nums[i];
27+
const slot = findSlot(num);
28+
sub[slot] = num;
29+
}
30+
31+
return sub.length;
32+
}

0 commit comments

Comments
 (0)