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.

这道题给了我们一个数组,让求最长递增序列的个数,题目中的两个例子也很好的说明了问题。那么对于这种求极值的问题,直觉告诉我们应该要使用动态规划 Dynamic Programming 来做。其实这道题在设计 DP 数组的时候有个坑,如果将 dp[i] 定义为到i位置的最长子序列的个数的话,则递推公式不好找。但是如果将 dp[i] 定义为以 nums[i] 为结尾的递推序列的个数的话,再配上这些递推序列的长度,将会比较容易的发现递推关系。这里用 len[i] 表示以 nums[i] 为结尾的递推序列的长度,用 cnt[i] 表示以 nums[i] 为结尾的递推序列的个数,初始化都赋值为1,只要有数字,那么至少都是1。然后遍历数组,对于每个遍历到的数字 nums[i],再遍历其之前的所有数字 nums[j],当 nums[i] 小于等于 nums[j] 时,不做任何处理,因为不是递增序列。反之,则判断 len[i] 和 len[j] 的关系,如果 len[i] 等于 len[j] + 1,说明 nums[i] 这个数字可以加在以 nums[j] 结尾的递增序列后面,并且以 nums[j] 结尾的递增序列个数可以直接加到以 nums[i] 结尾的递增序列个数上。如果 len[i] 小于 len[j] + 1,说明找到了一条长度更长的递增序列,那么此时将 len[i] 更新为 len[j]+1,并且原本的递增序列都不能用了,直接用 cnt[j] 来代替。在更新完 len[i] 和 cnt[i] 之后,要更新 mx 和结果 res,如果 mx 等于 len[i],则把 cnt[i] 加到结果 res 之上;如果 mx 小于 len[i],则更新 mx 为 len[i],更新结果 res 为 cnt[i],参见代码如下:

解法一:

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

下面这种方法跟上面的解法基本一样,就是把更新结果 res 放在了遍历完数组之后,我们利用 mx 来找到所有的 cnt[i],累加到结果 res 上,参见代码如下:

解法二:

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

Github 同步地址:

https://github.com/grandyang/leetcode/issues/673

类似题目:

Longest Increasing Subsequence

Longest Continuous Increasing Subsequence

参考资料:

https://leetcode.com/problems/number-of-longest-increasing-subsequence/

https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/107318/C%2B%2B-DP-with-explanation-O(n2)

https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/107293/JavaC%2B%2B-Simple-dp-solution-with-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数的更多相关文章

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

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

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

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

  3. LeetCode 673. Number of Longest Increasing Subsequence

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

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

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

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

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

  6. leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence

    Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...

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

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

  8. 673. Number of Longest Increasing Subsequence

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

  9. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

随机推荐

  1. 2019.11.21 做OJ题的反思

    1.利用二分法查找数组元素(适用于有序数组) #include<stdio.h> int BinarySearch(int a[],int n,int key); ]; int main( ...

  2. PHP高级进阶梳理

    基础篇 1.深入理解计算机系统 2.现代操作系统 3.C程序设计语言 4.C语言数据结构和算法 5.Unix环境高级编程 6.TCP/IP网络通信详解 7.Java面向对象编程 8.Java编程思想 ...

  3. 联合 CNCF 共同出品:Kubernetes and Cloud Native Meetup 成都站

    亮点解读 云原生前沿技术分享:阿里经济体“云原生化”宝贵经验与最佳实践成果 OpenKruise 价值几何? 防踩坑指南:国内知名容器平台架构师解读从 ECS 迁移到 K8S 走过哪些坑. ​云原生服 ...

  4. 机器学习(十)-------- 降维(Dimensionality Reduction)

    降维(Dimensionality Reduction) 降维的目的:1 数据压缩 这个是二维降一维 三维降二维就是落在一个平面上. 2 数据可视化 降维的算法只负责减少维数,新产生的特征的意义就必须 ...

  5. 自动轮播swiper css实现

    @keyframes scale { 0% { transform: scale(1, 1); opacity: 0.5; z-index:; transition: opacity z-index ...

  6. KVM学习笔记--静态迁移

    .静态迁移过程如下 (1)确定虚拟机关闭状态 (2)准备迁移oeltest02虚拟机,查看该虚拟机配置的磁盘文件 (3)导入虚拟机配置文件 [root@node1~]# virsh dumpxml o ...

  7. 一个JAVA应用启动缓慢问题排查 --来自jdk securerandom 的问候

    开发某个项目过程中,就需求,搭建了一套测试环境.很快完成! 后来代码中加入了许多新功能,会涉及到反复重启,然后就发现了启动特别慢.这给原本功能就不多的应用增添了许多的负担. 我决定改变这一切!找到启动 ...

  8. 【随笔】CLR:.net的类型,内部到底长啥样?

    前言 一提到.net的类型,首当其冲的就是“引用类型”.“值类型”:我们在面试中,也会经常被问“来说说值类型和引用类型....”,这时候第一反应就是:“哎呀,这还不简单,值类型是传递的值的copy,值 ...

  9. 数据库系统(五)---MySQL基础

    一.SQL基本概念: SQL 已经成为关系数据库的标准语言,是一种数据库查询和程序设计语言,用 于存取数据以及查询.更新和管理关系数据库系统. 功能不仅仅是查询,还包括数据定义.数据操纵和数据控制等于 ...

  10. jenkins如何构建C#代码写的网站

    纯粹是因为同事习惯了写C#代码,开发的网站用C#编译, 对于习惯了用Maven编译的测试人员,真是一头雾水.不用jenkins吧,效率特别低,每次收到开发发过来的版本,还要进行数据库相关配置,是非常累 ...