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. iOS 一个项目添加多个TARGET

    项目开发中会存在测试.正式等不同环境,需对应不同接口Host地址.项目名称等等配置.如果每次只有一个项目target的话每次打包的时候替换会很麻烦,而且容易出错.所以我们可以通过创建多个不同配置的ta ...

  2. Freemarker入门Demo

    1:工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...

  3. 统一批处理流处理——Flink批流一体实现原理

    实现批处理的技术许许多多,从各种关系型数据库的sql处理,到大数据领域的MapReduce,Hive,Spark等等.这些都是处理有限数据流的经典方式.而Flink专注的是无限流处理,那么他是怎么做到 ...

  4. 【MySQL】条件查询之排序聚合分组分页查询

    排序查询 语法:order by 子句 order by 排序字段1 排序方式1 , 排序字段2 排序方式2... 排序方式: ASC:升序,默认的. DESC:降序. 注意: 如果有多个排序条件,则 ...

  5. 三维网格细分算法(Catmull-Clark subdivision & Loop subdivision)附源码(转载)

    转载:  https://www.cnblogs.com/shushen/p/5251070.html 下图描述了细分的基本思想,每次细分都是在每条边上插入一个新的顶点,可以看到随着细分次数的增加,折 ...

  6. HashHelper

    在C#中,数据的Hash以MD5或SHA-1的方式实现,MD5与SHA1都是Hash算法,MD5输出是128位的,SHA1输出是160位的,MD5比SHA1快,SHA1比MD5强度高. MD5与SHA ...

  7. B-Tree详解

    之前写过一篇关于索引的文章<SQL夯实基础(五):索引的数据结构>,这次我们主要详细讨论下B-Tree. B-树 B-tree,即B树,而不要读成B减树,它是一种多路搜索树(并不是二叉的) ...

  8. 深入理解Vue组件3大核心概念

    摘要: 搞懂Vue组件! 作者:浪里行舟 原文:详解vue组件三大核心概念 Fundebug经授权转载,版权归原作者所有. 前言 本文主要介绍属性.事件和插槽这三个vue基础概念.使用方法及其容易被忽 ...

  9. 【转】聊一聊-JAVA 泛型中的通配符 T,E,K,V,?

    原文:https://juejin.im/post/5d5789d26fb9a06ad0056bd9 前言 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型 ...

  10. Android实用的Toast工具类封装

    Toast这个提示框大家都晓得,显示一段时间后自动消失,不能获得焦点.但是在使用中有些问题: 1)需要弹出一个新的Toast时,上一个Toast还没有显示完2)可能重复弹出相同的信息3)Toast具体 ...