当前位置:网站首页>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;
};
边栏推荐
- TCP/IP协议
- Trying to use is on a network resource that is unavailable
- 如何有效地进行自动化测试?
- win10系统中的截图,win+prtSc保存位置
- China vanadium battery Market Research and future prospects report (2022 Edition)
- 可变长参数
- C語言雙指針——經典題型
- Precise query of tree tree
- Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
- C语言深度解剖——C语言关键字
猜你喜欢

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

egg. JS getting started navigation: installation, use and learning

Simple use of promise in uniapp

TCP/IP协议

swagger设置字段required必填

Computer cleaning, deleted system files
![[brush questions] top101 must be brushed in the interview of niuke.com](/img/55/5ca957e65d48e19dbac8043e89e7d9.png)
[brush questions] top101 must be brushed in the interview of niuke.com

Navicat premium create MySQL create stored procedure

Current situation and trend of character animation

MySQL learning record 10getting started with JDBC
随机推荐
游戏解包的危害及资源加密的重要性
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
TDengine 社区问题双周精选 | 第三期
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
C语言深度解剖——C语言关键字
hutool优雅解析URL链接并获取参数
gcc动态库fPIC和fpic编译选项差异介绍
如何有效地进行自动化测试?
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
The network model established by torch is displayed by torch viz
swagger设置字段required必填
Deep anatomy of C language -- C language keywords
What is CSRF (Cross Site Request Forgery)?
Roguelike游戏成破解重灾区,如何破局?
win10系统中的截图,win+prtSc保存位置
生成器参数传入参数
TP-LINK 企业路由器 PPTP 配置
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
egg. JS directory structure
Navicat premium create MySQL create stored procedure