当前位置:网站首页>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;
};
边栏推荐
- How to conduct interface test? What are the precautions? Nanny level interpretation
- Crash problem of Chrome browser
- [today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
- 企微服务商平台收费接口对接教程
- Charging interface docking tutorial of enterprise and micro service provider platform
- [Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
- TDengine 社区问题双周精选 | 第三期
- What is CSRF (Cross Site Request Forgery)?
- 【剑指offer】序列化二叉树
- Browser thread
猜你喜欢
704 binary search
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
Promise 在uniapp的简单使用
LeetCode:236. 二叉树的最近公共祖先
【嵌入式】使用JLINK RTT打印log
LeetCode:498. 对角线遍历
visdom可视化实现与检查介绍
LeetCode:124. 二叉树中的最大路径和
Double pointeur en langage C - - modèle classique
UML diagram memory skills
随机推荐
TCP/IP协议
优秀的软件测试人员,都具备这些能力
电脑清理,删除的系统文件
The network model established by torch is displayed by torch viz
随手记01
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
Problems in loading and saving pytorch trained models
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
Roguelike game into crack the hardest hit areas, how to break the bureau?
深度剖析C语言数据在内存中的存储
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
[MySQL] multi table query
Using C language to complete a simple calculator (function pointer array and callback function)
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
Esp8266-rtos IOT development
Export IEEE document format using latex
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
超高效!Swagger-Yapi的秘密