LeetCode 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence.
Example 1:
Input: [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input: [2,2,2,2,2]
Output: 5
Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
分析
这题和求最长不降子列类似,dp[i]记录以nums[i]结尾的最长子列长度,只需要另一个数组cnt,cnt[i]记录dp[i]对应的有几种,longest记录全程中最长子列长度,最后遍历dp数组,当dp[i]==longest时,count+=cnt[i].
class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
if(nums.size()==0) return 0;
int count=0,longest=1;
vector<int> dp(nums.size(),1),cnt(nums.size(),1);
for(int i=1;i<nums.size();i++){
for(int j=i-1;j>=0;j--){
if(nums[i]>nums[j]){
if(dp[j]+1>dp[i]){
dp[i]=dp[j]+1;
cnt[i]=cnt[j];
}else if(dp[j]+1==dp[i])
cnt[i]+=cnt[j];
}
}
longest=max(longest,dp[i]);
}
for(int i=0;i<nums.size();i++)
if(dp[i]==longest) count+=cnt[i];
return count;
}
};
LeetCode 673. Number of Longest Increasing Subsequence的更多相关文章
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- Week 12 - 673.Number of Longest Increasing Subsequence
Week 12 - 673.Number of Longest Increasing Subsequence Given an unsorted array of integers, find the ...
- 【LeetCode】673. Number of Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...
- 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- LeetCode Number of Longest Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...
- [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
随机推荐
- laravel配合swoole使用总结
最近对接硬件做了两个项目,用到了swoole 第一个是门禁系统,需要远程开门.离线报警.定时开门.离线刷卡等功能 1.远程开门: 目前用cli创建个临时客户端连接服务端发送命令,服务端处理完成后客户端 ...
- python简单脚本-sql字符提取
a="""dr.GetStr("kh"), dr.GetStr("xm"), dr.GetStr("xh"), ...
- html5基础知识学习
HTML5腾讯课堂--html5前端开发视频 https://ke.qq.com/course/89671 html5_video http://www.myexception.cn/jquery/2 ...
- Java程序流程控制之if-else if-else
java基础之流程控制(一) 流程控制 Flow Control : 流程控制语句是编程语言中的核心之一.可以分为 分支语句.循环语句和跳转语句. 本讲内容包括分支语句 ...
- ES6中新增的字符串方法
实例方法:includes(), startsWith(), endsWith() 传统上,JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6 又提供 ...
- 2566. [51nod 1129] 字符串最大值
[题目描述] 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd. 给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值. 例 ...
- 在页面实现qq跳转链接
http://shang.qq.com/v3/widget/consult.html
- JSP界面设置提示浮动框
1.公共js <script type="text/javascript"> var tip={ $:function(ele){ if(typeof(ele)==&q ...
- 深入浅出Android动态加载jar包技术
在实际项目中,由于某些业务频繁变更而导致频繁升级客户端的弊病会造成较差的用户体验,而这也恰是Web App的优势,于是便衍生了一种思路,将核心的易于变更的业务封装在jar包里然后通过网络下载下来,再由 ...
- git的基本使用命令操作
Linux操作命令行: mkdir - 创建文件夹, cd - 切换文件路径 pwd - 显示文件路径 ls -ah - 可以查看隐藏的文件夹名(.git) cat 文件 ...