From 5a10b329da87245d11a46564af8e4cb00c122113 Mon Sep 17 00:00:00 2001 From: jerryfishcode <91447694+jerryfishcode@users.noreply.github.com> Date: Mon, 27 Sep 2021 20:48:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0673.=20=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E9=80=92=E5=A2=9E=E5=AD=90=E5=BA=8F=E5=88=97=E7=9A=84=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=20JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0673.最长递增子序列的个数.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0673.最长递增子序列的个数.md b/problems/0673.最长递增子序列的个数.md index 653edadf..bf0a2a5a 100644 --- a/problems/0673.最长递增子序列的个数.md +++ b/problems/0673.最长递增子序列的个数.md @@ -337,6 +337,28 @@ func findNumberOfLIS(nums []int) int { ## JavaScript ```js +var findNumberOfLIS = function(nums) { + const len = nums.length; + if(len <= 1) return len; + let dp = new Array(len).fill(1); // i之前(包括i)最长递增子序列的长度为dp[i] + let count = new Array(len).fill(1); // 以nums[i]为结尾的字符串,最长递增子序列的个数为count[i] + let res = 0; + for(let i = 1; i < len; i++){ + for(let j = 0; j < i; j++){ + if(nums[i] > nums[j]){ + if(dp[j] + 1 > dp[i]){ // 第 j 个数字为前一个数字的子序列是否更更长 + dp[i] = dp[j] + 1; //更新 dp[i] + count[i] = count[j]; // 重置count[i] + } else if(dp[j] + 1 === dp[i]){ // 和原来一样长 + count[i] += count[j]; //更新count[i] + } + } + } + } + let max = Math.max(...dp); //扩展运算符找到最大长度 + for(let i = 0; i < len; i++) if(dp[i] === max) res += count[i]; // 累加 + return res; +}; ``` -----------------------