当前位置:网站首页>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;
};
边栏推荐
- 深度剖析C语言数据在内存中的存储
- Trying to use is on a network resource that is unavailable
- 游戏解包的危害及资源加密的重要性
- Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
- Deep anatomy of C language -- C language keywords
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- swagger设置字段required必填
- PC easy to use essential software (used)
- 个人电脑好用必备软件(使用过)
- sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
猜你喜欢

Double pointeur en langage C - - modèle classique

Mobile phones and computers on the same LAN access each other, IIS settings

优秀的软件测试人员,都具备这些能力

Deep analysis of C language pointer

深度剖析C语言指针

Image, CV2 read the conversion and size resize change of numpy array of pictures

TP-LINK enterprise router PPTP configuration

同一局域网的手机和电脑相互访问,IIS设置

【嵌入式】Cortex M4F DSP库

pytorch训练好的模型在加载和保存过程中的问题
随机推荐
Golang force buckle leetcode 1020 Number of enclaves
[MySQL] log
Sort according to a number in a string in a column of CSV file
Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
Simple use of promise in uniapp
Sublime text using ctrl+b to run another program without closing other runs
The network model established by torch is displayed by torch viz
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
Colorlog combined with logging to print colored logs
China vanadium battery Market Research and future prospects report (2022 Edition)
MySQL learning record 07 index (simple understanding)
torch建立的网络模型使用torchviz显示
704 binary search
TP-LINK 企业路由器 PPTP 配置
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
Detailed explanation of heap sorting
Variable length parameter
深度剖析C语言数据在内存中的存储
vb. Net changes with the window, scales the size of the control and maintains its relative position
Is it safe to open an account in Zheshang futures?