Skip to content

Commit 0145d19

Browse files
committed
Create 300.最长上升子序列.js
1 parent 57c9ebb commit 0145d19

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

300.最长上升子序列.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var lengthOfLIS = function(nums) {
6+
if (nums.length <= 1) {
7+
return nums.length;
8+
}
9+
10+
const dp = [1];
11+
let result = 0;
12+
for (let i = 1; i < nums.length; i++) {
13+
dp[i] = 1;
14+
for (let j = 0; j < i; j++) {
15+
if (nums[j] < nums[i]) {
16+
dp[i] = Math.max(dp[i], dp[j] + 1);
17+
}
18+
}
19+
result = Math.max(result, dp[i]);
20+
}
21+
return result;
22+
};

0 commit comments

Comments
 (0)