-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
89 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
function longestIncreasingSubsequence(nums) { | ||
const dp = Array(nums.length).fill(1); // dp数组记录每个位置的LIS长度 | ||
const prev = Array(nums.length).fill(-1); // prev数组记录每个位置的前一个索引 | ||
let maxIndex = 0; // 用于追踪最长序列的结束索引 | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
for (let j = 0; j < i; j++) { | ||
if (nums[i] > nums[j] && dp[i] < dp[j] + 1) { | ||
dp[i] = dp[j] + 1; | ||
prev[i] = j; // 更新前一个元素索引 | ||
} | ||
} | ||
if (dp[i] > dp[maxIndex]) { | ||
maxIndex = i; // 更新最长序列的结束位置 | ||
} | ||
} | ||
|
||
// 回溯构造LIS | ||
const lis = []; | ||
while (maxIndex !== -1) { | ||
lis.unshift(nums[maxIndex]); | ||
maxIndex = prev[maxIndex]; | ||
} | ||
|
||
return lis; | ||
} | ||
|
||
// 测试 | ||
const nums = [5, 19, 5, 81, 50, 28, 29, 1, 83, 23]; | ||
console.log(longestIncreasingSubsequence(nums)); // 输出 [2, 3, 7, 101] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters