当前位置:网站首页>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;
};
边栏推荐
猜你喜欢

Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school

【嵌入式】Cortex M4F DSP库

Delay initialization and sealing classes

Detailed explanation of dynamic planning

Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures

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

marathon-envs项目环境配置(强化学习模仿参考动作)
![[MySQL] limit implements paging](/img/94/2e84a3878e10636460aa0fe0adef67.jpg)
[MySQL] limit implements paging

LeetCode:221. 最大正方形

Marathon envs project environment configuration (strengthen learning and imitate reference actions)
随机推荐
电脑清理,删除的系统文件
[sword finger offer] serialized binary tree
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
C语言深度解剖——C语言关键字
Niuke winter vacation training 6 maze 2
LeetCode:剑指 Offer 42. 连续子数组的最大和
Philosophical enlightenment from single point to distributed
有效提高软件产品质量,就找第三方软件测评机构
win10系统中的截图,win+prtSc保存位置
Deep analysis of C language pointer
Trying to use is on a network resource that is unavailable
Navicat premium create MySQL create stored procedure
Variable length parameter
marathon-envs项目环境配置(强化学习模仿参考动作)
The harm of game unpacking and the importance of resource encryption
个人电脑好用必备软件(使用过)
力扣每日一题(二)
MYSQL卸载方法与安装方法
Revit 二次开发 HOF 方式调用transaction
LeetCode:26. 删除有序数组中的重复项