Skip to content

Commit 93a584f

Browse files
committed
feat: 300. Longest Increasing Subsequence
1 parent 3f2b2ec commit 93a584f

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+
// n: len(nums)
2+
// Time complexity: O(n^2)
3+
// Space complexity: O(n)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @return {number}
8+
*/
9+
var lengthOfLIS = function (nums) {
10+
const dp = Array.from({ length: nums.length }, () => 1);
11+
12+
for (let i = 1; i < nums.length; i++) {
13+
for (let j = 0; j < i; j++) {
14+
if (nums[j] < nums[i]) {
15+
dp[i] = Math.max(dp[i], dp[j] + 1);
16+
}
17+
}
18+
}
19+
20+
return Math.max(...dp);
21+
};

0 commit comments

Comments
 (0)