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

visdom可视化实现与检查介绍

Mongodb installation and basic operation

Current situation and trend of character animation

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

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

pytorch训练好的模型在加载和保存过程中的问题

Process of obtaining the electronic version of academic qualifications of xuexin.com

Screenshot in win10 system, win+prtsc save location

Detailed explanation of heap sorting

Detailed explanation of dynamic planning
随机推荐
Problems in loading and saving pytorch trained models
Light of domestic games destroyed by cracking
[MySQL] limit implements paging
Super efficient! The secret of swagger Yapi
sublime text中conda环境中plt.show无法弹出显示图片的问题
To effectively improve the quality of software products, find a third-party software evaluation organization
Using C language to complete a simple calculator (function pointer array and callback function)
The network model established by torch is displayed by torch viz
Unsupported operation exception
LeetCode:124. 二叉树中的最大路径和
随手记01
Trying to use is on a network resource that is unavailable
Bitwise logical operator
marathon-envs项目环境配置(强化学习模仿参考动作)
Roguelike game into crack the hardest hit areas, how to break the bureau?
How to effectively conduct automated testing?
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
Pytorch view tensor memory size
Process of obtaining the electronic version of academic qualifications of xuexin.com
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