当前位置:网站首页>LeetCode:673. Number of longest increasing subsequences
LeetCode:673. Number of longest increasing subsequences
2022-07-06 08:50:00 【Bertil】
Given an array of unsorted integers nums , Returns the number of longest increasing subsequences .
Be careful This sequence must be Strictly Incremental .
Example 1:
Input : [1,3,5,4,7]
Output : 2
explain : There are two longest increasing subsequences , Namely [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input : [2,2,2,2,2]
Output : 5
explain : The length of the longest increasing subsequence is 1, And it exists 5 The length of the subsequence is 1, So output 5.
Tips :
- 1 <= nums.length <= 2000
- -10^6 <= nums[i] <= 10^6
Their thinking
1. First initialize two arrays : state dp[i] Said to nums[i] The length of the longest ascending subsequence at the end ,count[i] Said to nums[i] Number of longest ascending subsequences at the end
2. Then find the longest subsequence and mark it
3. Finally, count the number of occurrences of the longest subsequence and return
Code
/** * @param {number[]} nums * @return {number} */
var findNumberOfLIS = function(nums) {
// initialization dp Array
let len = nums.length;
let dp = Array(len).fill(1);
let count = Array(len).fill(1);
let res = 0;
// Find the longest subsequence and mark it
for(let i = 0; i < len; i++) {
for(let j = 0; j < i; j++) {
if(nums[i] > nums[j]) {
// Math.max... Writing
if(dp[j]+1 > dp[i]) {
dp[i] = dp[j]+1
count[i] = count[j] // The number of longest increasing subsequences
// In fact, it is to consider the equal situation
}else if(dp[j]+1 === dp[i]) {
count[i] += count[j]
}
}
}
}
// Statistics max Number of occurrences
let max = Math.max(...dp);
// Here I want to count all LIS The number of must be traversed
for (let i = 0; i < len; i ++) if(dp[i] === max) res += count[i];
return res;
};
边栏推荐
- Function coritization
- 深度剖析C语言数据在内存中的存储
- Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
- Super efficient! The secret of swagger Yapi
- Shift Operators
- Mongodb installation and basic operation
- LeetCode:劍指 Offer 42. 連續子數組的最大和
- Bitwise logical operator
- Cesium draw points, lines, and faces
- 软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
猜你喜欢
MongoDB 的安装和基本操作
vb. Net changes with the window, scales the size of the control and maintains its relative position
【ROS】usb_ Cam camera calibration
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
egg. JS getting started navigation: installation, use and learning
704 binary search
【嵌入式】使用JLINK RTT打印log
UML图记忆技巧
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
生成器参数传入参数
随机推荐
UnsupportedOperationException异常
企微服务商平台收费接口对接教程
egg. JS directory structure
[MySQL] multi table query
TCP/IP协议
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
Export IEEE document format using latex
CSP first week of question brushing
优秀的软件测试人员,都具备这些能力
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
Tcp/ip protocol
Mobile phones and computers on the same LAN access each other, IIS settings
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
[sword finger offer] serialized binary tree
Image, CV2 read the conversion and size resize change of numpy array of pictures
gcc动态库fPIC和fpic编译选项差异介绍
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
JS pure function
View computer devices in LAN
【嵌入式】Cortex M4F DSP库