Skip to content

Commit 36e7cf3

Browse files
committed
Add week 6 solutions: longest-increasing-subsequence
1 parent 8382887 commit 36e7cf3

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+
* https://leetcode.com/problems/longest-increasing-subsequence
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
function lengthOfLIS(nums: number[]): number {
7+
const [head] = nums;
8+
const basket = [head];
9+
10+
for (let i = 1; i < nums.length; i++) {
11+
const current = nums[i];
12+
let j = 0;
13+
14+
while (j < basket.length && basket[j] < current) j++;
15+
16+
if (j === basket.length) basket.push(current);
17+
else basket[j] = current;
18+
}
19+
20+
return basket.length;
21+
};

0 commit comments

Comments
 (0)