Given an unsorted array of integers, find the number of longest increasing subsequence.

Example 1:

  1. Input: [1,3,5,4,7]
  2. Output: 2
  3. Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].

Example 2:

  1. Input: [2,2,2,2,2]
  2. Output: 5
  3. 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].

  1. class Solution {
  2. public:
  3. int findNumberOfLIS(vector<int>& nums) {
  4. if(nums.size()==0) return 0;
  5. int count=0,longest=1;
  6. vector<int> dp(nums.size(),1),cnt(nums.size(),1);
  7. for(int i=1;i<nums.size();i++){
  8. for(int j=i-1;j>=0;j--){
  9. if(nums[i]>nums[j]){
  10. if(dp[j]+1>dp[i]){
  11. dp[i]=dp[j]+1;
  12. cnt[i]=cnt[j];
  13. }else if(dp[j]+1==dp[i])
  14. cnt[i]+=cnt[j];
  15. }
  16. }
  17. longest=max(longest,dp[i]);
  18. }
  19. for(int i=0;i<nums.size();i++)
  20. if(dp[i]==longest) count+=cnt[i];
  21. return count;
  22. }
  23. };

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. 洛谷1005(dp)

    1.不要贪,缩小区间去dp就好. 2.预处理指数. 3.__int128可还行. #include <cstdio> #include <cctype> #include &l ...

  2. Maximum Control (medium) Codeforces - 958B2

    https://codeforces.com/contest/958/problem/B2 题解:https://www.cnblogs.com/Cool-Angel/p/8862649.html u ...

  3. D - 連結 / Connectivity 并查集

    http://abc049.contest.atcoder.jp/tasks/arc065_b 一开始做这题的时候,就直接蒙逼了,n是2e5,如果真的要算出每一个节点u能否到达任意一个节点i,这不是f ...

  4. Nginx pathinfo模式配置

    正常配置 location ~ \.php$ { fastcgi_pass ; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $docu ...

  5. jQuery测试

    1.在div元素中,包含了一个<span>元素,通过has选择器获取<div>元素中的<span>元素的语法是? 提示使用has() $("div&quo ...

  6. 完成FileUpload的文件上传功能,且可改按钮样式

    FileUpload控件: 更改按钮样式思路: 自己定义一个按钮,设置该按钮的样式,然后将FileUpload控件通过定位定在自己定义的按钮上面,设置z-index,使得控件浮在自己定义的按钮上面,记 ...

  7. 一步步实现自己的ORM(四)

    通过前3章文章,大致对ORM有一定的了解,但也存在效率低下(大量用了反射)和重复代码,今天我们要对ORM进行优化. 具体流程如下: 我们优化的第一个就是减少反射调用,我的思路是定义一个Mapping, ...

  8. 前端之css(宽高)设置小技巧

    一.css宽高自适应: 1.宽度自适应: 元素宽度设为100%(块状元素的默认宽度为100%) 注:应用在通栏效果中 2.高度自适应: height:auto;或者不设置高度 3.最小,最大高度,最小 ...

  9. System.TypeInitializationException: 'The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception.'

    下午在调试的时候报错数据库连接就报错我就很纳闷后面用原来的代码写发现还是报错 System.TypeInitializationException: 'The type initializer for ...

  10. uvm_marcos——UVM宏定义

    I programmed all night.Through the window, on my screen,The rising sun shined. 编程一整夜,透过窗户,照在屏幕上.初升的太 ...