当前位置:网站首页>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;
};
边栏推荐
- Screenshot in win10 system, win+prtsc save location
- Current situation and trend of character animation
- 如何有效地进行自动化测试?
- [NVIDIA development board] FAQ (updated from time to time)
- 软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
- LeetCode:39. 组合总和
- poi追加写EXCEL文件
- Delay initialization and sealing classes
- Detailed explanation of dynamic planning
- Promise 在uniapp的简单使用
猜你喜欢
Compétences en mémoire des graphiques UML
Charging interface docking tutorial of enterprise and micro service provider platform
JS native implementation shuttle box
优秀的软件测试人员,都具备这些能力
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
Navicat Premium 创建MySql 创建存储过程
C語言雙指針——經典題型
同一局域网的手机和电脑相互访问,IIS设置
【嵌入式】使用JLINK RTT打印log
egg. JS getting started navigation: installation, use and learning
随机推荐
@JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
Export IEEE document format using latex
The mysqlbinlog command uses
Purpose of computer F1-F12
ESP8266-RTOS物联网开发
vb.net 随窗口改变,缩放控件大小以及保持相对位置
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
LeetCode:剑指 Offer 03. 数组中重复的数字
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
JS inheritance method
超高效!Swagger-Yapi的秘密
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
Mongodb installation and basic operation
C语言深度解剖——C语言关键字
The network model established by torch is displayed by torch viz
使用latex导出IEEE文献格式
Revit 二次开发 HOF 方式调用transaction
查看局域网中电脑设备
LeetCode:394. 字符串解码