Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit64673f2

Browse files
Merge pull requestyoungyangyang04#492 from jackeyjia/patch-12
add js solution for lengthOfLIS
2 parents81f74d8 +f7bfc05 commit64673f2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎problems/0300.最长上升子序列.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ func lengthOfLIS(nums []int ) int {
170170
returnlen(dp)
171171
}
172172
```
173+
174+
Javascript
175+
```javascript
176+
constlengthOfLIS= (nums)=> {
177+
let dp=Array(nums.length).fill(1);
178+
let result=1;
179+
180+
for(let i=1; i<nums.length; i++) {
181+
for(let j=0; j< i; j++) {
182+
if(nums[i]> nums[j]) {
183+
dp[i]=Math.max(dp[i], dp[j]+1);
184+
}
185+
}
186+
result=Math.max(result, dp[i]);
187+
}
188+
189+
return result;
190+
};
191+
```
173192
*复杂度分析*
174193
- 时间复杂度:O(nlogn)。数组 nums 的长度为 n,我们依次用数组中的元素去更新 dp 数组,相当于插入最后递增的元素,而更新 dp 数组时需要进行 O(logn) 的二分搜索,所以总时间复杂度为 O(nlogn)。
175194
- 空间复杂度:O(n),需要额外使用长度为 n 的 dp 数组。

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp