当前位置:网站首页>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;
};
边栏推荐
- MySQL learning record 07 index (simple understanding)
- View computer devices in LAN
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
- [MySQL] log
- Generator parameters incoming parameters
- Tcp/ip protocol
- pytorch训练好的模型在加载和保存过程中的问题
- TDengine 社区问题双周精选 | 第三期
- Process of obtaining the electronic version of academic qualifications of xuexin.com
猜你喜欢

【ROS】usb_ Cam camera calibration
![[embedded] print log using JLINK RTT](/img/22/c37f6e0f3fb76bab48a9a5a3bb3fe5.png)
[embedded] print log using JLINK RTT

ESP8266-RTOS物联网开发

软件卸载时遇到trying to use is on a network resource that is unavailable

egg. JS project deployment online server

View computer devices in LAN

What is CSRF (Cross Site Request Forgery)?

JS native implementation shuttle box

swagger设置字段required必填

Deep analysis of C language data storage in memory
随机推荐
Sort according to a number in a string in a column of CSV file
vb.net 随窗口改变,缩放控件大小以及保持相对位置
torch建立的网络模型使用torchviz显示
Shift Operators
Esp8266-rtos IOT development
egg. JS directory structure
logback1.3. X configuration details and Practice
ESP8266-RTOS物联网开发
Simple use of promise in uniapp
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
Double pointeur en langage C - - modèle classique
The harm of game unpacking and the importance of resource encryption
Bottom up - physical layer
Unified ordering background interface product description Chinese garbled
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
Generator parameters incoming parameters
2022.02.13 - NC002. sort
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
Browser thread