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的更多相关文章

  1. [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  2. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  3. Week 12 - 673.Number of Longest Increasing Subsequence

    Week 12 - 673.Number of Longest Increasing Subsequence Given an unsorted array of integers, find the ...

  4. 【LeetCode】673. Number of Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...

  5. 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  6. 673. Number of Longest Increasing Subsequence最长递增子序列的数量

    [抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...

  7. [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  8. LeetCode Number of Longest Increasing Subsequence

    原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...

  9. [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

随机推荐

  1. 关于presentViewController 后调用pushViewController

    错误代码: LoginViewController *loginVc = [[LoginViewController alloc] int]; [self presentViewController ...

  2. 计算机中如何实现除数是2的幂次的除法【转载自CSDN】

    前言: 本来是在看汇编里面的数据条件传送指令,做习题的时候看着这么一道有关于2的幂次方除法的题目.结果傻眼了,又尼玛不会了.........第二章看的时候就稀里糊涂的,看了几遍也没看太懂,这回又涉及到 ...

  3. Excel 通过pl/sql导入到数据库 文本导入器 odbc导入器

     Excel 通过pl/sql导入到数据库 第一种方法:文本导入器 1.准备Excel导入数据   jc.xls 2.把 jc.xls 文件 改为 jc.csv文件 3.在数据库里建一张jc表(FLH ...

  4. ZOJ Saddle Point 数学思维题

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5564   根据它的定义是行最小,列最大. 可以证明鞍点是唯一的. ...

  5. Storm编程入门API系列之Storm的Topology多个tasks数目控制实现

    前期博客 Storm编程入门API系列之Storm的Topology默认Workers.默认executors和默认tasks数目 Storm编程入门API系列之Storm的Topology多个Wor ...

  6. CImage访问像素及其像素操作总结

    MSDN的代码 COLORREF pixel; int maxY = imgOriginal.GetHeight(), maxX = imgOriginal.GetWidth(); byte r,g, ...

  7. Android Studio项目上传到Jcenter

    一.将你要发布的moudle的build.gradle中添加代码,gradle的最后添加 PUBLISH_GROUP_ID = 'com.zzti.fengyongge' PUBLISH_ARTIFA ...

  8. 洛谷 P1168 中位数

    题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[3], …, A[2k - 1]的中位数.[color=red]即[/color] ...

  9. 洛谷 P1996 约瑟夫问题

    题目背景 约瑟夫是一个无聊的人!!! 题目描述 n个人(n<=100)围成一圈,从第一个人开始报数,数到m的人出列,再由下一个人重新从1开始报数,数到m的人再出圈,……依次类推,直到所有的人都出 ...

  10. 如何处理Docker错误消息:please add——insecure-registry

    本地安装Kubernetes时,遇到如下的错误消息: pleade add --insecure-registry gcr.io to daemon's arguments 解决方案:点击Docker ...