当前位置:网站首页>LeetCode:673. 最长递增子序列的个数
LeetCode:673. 最长递增子序列的个数
2022-07-06 08:44:00 【Bertil】
给定一个未排序的整数数组 nums , 返回最长递增子序列的个数 。
注意 这个数列必须是 严格 递增的。
示例 1:
输入: [1,3,5,4,7]
输出: 2
解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。
示例 2:
输入: [2,2,2,2,2]
输出: 5
解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。
提示:
- 1 <= nums.length <= 2000
- -10^6 <= nums[i] <= 10^6
解题思路
1.首先初始化两个数组:状态dp[i]表示以nums[i] 结尾的最长上升子序列的长度,count[i] 表示以nums[i] 结尾的最长上升子序列的个数
2.然后找到最长子序列并且做好标记
3.最后统计最长子序列出现的次数并且返回
代码
/** * @param {number[]} nums * @return {number} */
var findNumberOfLIS = function(nums) {
// 初始化dp数组
let len = nums.length;
let dp = Array(len).fill(1);
let count = Array(len).fill(1);
let res = 0;
// 找到最长子序列并做好标记
for(let i = 0; i < len; i++) {
for(let j = 0; j < i; j++) {
if(nums[i] > nums[j]) {
// Math.max...的写法
if(dp[j]+1 > dp[i]) {
dp[i] = dp[j]+1
count[i] = count[j] // 最长递增子序列的个数
// 其实就是考虑相等的情况
}else if(dp[j]+1 === dp[i]) {
count[i] += count[j]
}
}
}
}
// 统计一下 max 出现的次数
let max = Math.max(...dp);
// 这里想要统计所有LIS的个数就要遍历
for (let i = 0; i < len; i ++) if(dp[i] === max) res += count[i];
return res;
};
边栏推荐
- vb.net 随窗口改变,缩放控件大小以及保持相对位置
- swagger设置字段required必填
- Simple use of promise in uniapp
- Function coritization
- MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
- TP-LINK 企业路由器 PPTP 配置
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- Delay initialization and sealing classes
- ESP8266-RTOS物联网开发
- FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
猜你喜欢

2022.02.13 - NC001. Reverse linked list

pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof

Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines

Deep anatomy of C language -- C language keywords

Current situation and trend of character animation

Promise 在uniapp的简单使用

Visual implementation and inspection of visdom
![[embedded] print log using JLINK RTT](/img/22/c37f6e0f3fb76bab48a9a5a3bb3fe5.png)
[embedded] print log using JLINK RTT

swagger设置字段required必填

Sort according to a number in a string in a column of CSV file
随机推荐
[MySQL] lock
JVM 快速入门
MySQL learning records 12jdbc operation transactions
被破解毁掉的国产游戏之光
有效提高软件产品质量,就找第三方软件测评机构
JS inheritance method
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
hutool优雅解析URL链接并获取参数
如何进行接口测试测?有哪些注意事项?保姆级解读
gcc动态库fPIC和fpic编译选项差异介绍
Deep anatomy of C language -- C language keywords
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
POI add write excel file
704 binary search
Verrouillage [MySQL]
移位运算符
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
JVM quick start
The harm of game unpacking and the importance of resource encryption
Marathon envs project environment configuration (strengthen learning and imitate reference actions)