当前位置:网站首页>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 language double pointer -- classic question type
- C語言雙指針——經典題型
- Light of domestic games destroyed by cracking
- 深度剖析C语言指针
- How to effectively conduct automated testing?
- Computer cleaning, deleted system files
- FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
- POI add write excel file
- 2022.02.13 - 238. Maximum number of "balloons"
- 按位逻辑运算符
猜你喜欢

Restful API design specification

ESP8266-RTOS物联网开发

Indentation of tabs and spaces when writing programs for sublime text

Roguelike game into crack the hardest hit areas, how to break the bureau?

Detailed explanation of heap sorting

查看局域网中电脑设备

堆排序详解

704 binary search

【MySQL】鎖

JVM performance tuning and practical basic theory - Part 1
随机推荐
Navicat premium create MySQL create stored procedure
[NVIDIA development board] FAQ (updated from time to time)
ROS compilation calls the third-party dynamic library (xxx.so)
Sort according to a number in a string in a column of CSV file
What are the common processes of software stress testing? Professional software test reports issued by companies to share
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
被破解毁掉的国产游戏之光
有效提高软件产品质量,就找第三方软件测评机构
优秀的软件测试人员,都具备这些能力
egg. JS project deployment online server
Visual implementation and inspection of visdom
[MySQL] log
LeetCode:162. 寻找峰值
Delay initialization and sealing classes
The mysqlbinlog command uses
Sublime text using ctrl+b to run another program without closing other runs
JVM 快速入门
Deep analysis of C language pointer
可变长参数
Screenshot in win10 system, win+prtsc save location