题目:

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.

题解:

  首先解决最长的递增序列问题,最朴素的做法是深搜,以每一个数为开头,找位置在它后面的且数值比它大的为下一层,显然会超时,考虑用动态规划去解决问题(也就是最长上升序列(LIS),一个经典的动态规划问题)。其实就是LIS的变体。 摘自九章算法

1. 设dp[i]为以该数结尾,能构成的最长序列的长度。进行连接的时候,对于每个数字num[i],遍历位置在它之前的数字num[j],如果比这个数小(num[j]<num[i]),也就是能构成一个序列,这样就能进行状态转移,我们令dp[i]=max(dp[i],dp[j]+1)来保证存储的为最长长度,同时可以记录max(dp[i])

2. 考虑完题目的长度优先后,我们考虑数量,也就是说最长长度的序列有几个,这个问题需要我们在处理dp的时候来记录,我们设ans[i]为以第i个数结尾的最长序列的个数,与dp同理,ans初值也都是1

3. 状态转移的时候,如果dp更新了,也就是说(dp[j]+1>dp[i])说明这个长度的序列是新出现的,我们需要将ans[i]设置为ans[j],因为新序列中,最新的数提供了序列的尾巴,数量是由前面积累的(或者说转移);举例序列[1 1 3 7]我们易得数字3对应的dp=2,ans=2,因为可以构成两个[1 3]那么我们操作到数字7的时候,发现接在3后面最长,就可以转移ans来作为初始数量

4. 而当dp[j]+1==dp[i]的时候,如同样例,操作7的时候,我们最先发现了可以接在5后面,最长序列[1 3 5 7],然后发现可以接在4后面,[1 3 4 7],长度也是4,这时候就同样需要转移ans,加上去 ans[i]+=ans[j]

5. 最后我们需要遍历dp,找到dp[i]=我们记录的最大值的时候,累加我们得到的ans[i],即为所求结果,时间复杂度是O(n^2)

Solution

class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size(), max_len = , res = ;
vector<int> dp(n, ), cnt(n, );
for(int i = ; i < n; ++i){
for(int j = ; j < i; ++j){
if(nums[j] < nums[i] && dp[j] + > dp[i]){
dp[i] = dp[j] + ;
cnt[i] = cnt[j];
} else if(nums[j] < nums[i] && dp[j] + == dp[i]){
cnt[i] += cnt[j];
}
}
max_len = max(max_len, dp[i]);
}
for(int i = ; i < n; ++i)
if(dp[i] == max_len) res += cnt[i];
return res;
}
};

【LeetCode】673. Number of Longest Increasing Subsequence的更多相关文章

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

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

  2. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  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 1: I ...

  5. LeetCode 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. Example 1: I ...

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

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

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

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

  9. LeetCode Number of Longest Increasing Subsequence

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

随机推荐

  1. 手机端的META差异

    手机端的META你了解多少? 我们先来简单了解下meta标签:meta指元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. 标签位于文档的头部, ...

  2. 【leetcode刷题笔记】Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  3. Django ORM --- 建表、查询、删除基础

    1.什么是ORM ORM的全称是Object Relational Mapping,即对象关系映射.它的实现思想就是将关系数据库中表的数据映射成为对象,以对象的形式展现,这样开发人员就可以把对数据库的 ...

  4. EG:nginx反向代理两台web服务器,实现负载均衡 所有的web服务共享一台nfs的存储

    step1: 三台web服务器环境配置:iptables -F; setenforce 0 关闭防火墙:关闭setlinux step2:三台web服务器 装软件 step3: 主机修改配置文件:vi ...

  5. bootstrap 模态框中弹出层 input不能获得焦点且不可编辑

    bootstrap 模态框中弹出层 input不能获得焦点且不可编辑 问题描述:bs框架支持一层model层的情况下,在模态框中弹出了自定义的弹出层.发现自定义弹出层的输入框不能获得焦点且不可编辑. ...

  6. 关于 kinect 的开发

    1. 参考开发博客:http://www.cnblogs.com/yangecnu/p/Learning-KinectSDK.html

  7. 一元多项式的乘法与加法运算 【STL-map哈希-map反向迭代器遍历 + 零多项式】

    设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. ...

  8. RHCE学习笔记 管理1 (第一、二章)

    第一章 命令行访问 1.Ctrl+alt+F2~F6 切到虚拟控制台,ctrl+alt+F1 回到图形界面 2.格式 : 命令 选项 参数 [] 为可选项目            ...表示该项目任意 ...

  9. space sniffer清理的空间

    部分超级大的单文件,比如数据库 C:\inetpub\logs\LogFiles\W3SVC4 C:\Users\clu\AppData\Local\JetBrains\Transient C:\Us ...

  10. Luogu-1527 [国家集训队]矩阵乘法

    Luogu-1527 [国家集训队]矩阵乘法 题面 Luogu-1527 题解 昨天学CDQ分治时做了一些题,但是因为题(wo)太(tai)水(lan)了(le)并没有整理 学了一晚上的整体二分,拿这 ...