Number of Longest Increasing Subsequence

update Sep 13,2017 23:09

LeetCodearrow-up-right

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.

Basic Idea:

基本思想和 LIS 类似,只是需要在之前记录以每个元素结尾的最长子序列的数组之外,再配一个储存以每个元素结尾的最长子序列个数的数组。如此一来,最终的解就是最终 LIS 结尾元素所对应的个数)。

此题实现起来不容易,有很多细节需要注意,特别是当input array 所有元素都相同的时候。

Java Code:

Python Code:

Last updated