Number of Longest Increasing Subsequence
https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/
Given an unsorted array of integers, find the number of longest increasing subsequence.
Thoughts
在LIS的基础上要求返回达到最长长度的subseq的个数。我们可以在LIS的基础上多存储一个以nums[i]为结尾的最长subseq的个数,这个值可以在遍历j时得到。
设 Gi: the number of increasing subsequences ending with nums[i] that has len Fi.
注意这道题不能用LIS的方法做. 用题目给的例子[1,3,5,4,7], tails为
[1, 0, 0, 0, 0]
[1, 3, 0, 0, 0]
[1, 3, 5, 0, 0]
[1, 3, 4, 0, 0]
[1, 3, 4, 7, 0]
只知道长度为4的LIS末尾最小值是7, 但不知道前面有多少种可能性, 如1347, 1357等等. 注意tails里的1,3,4,7不一定是条真的LIS.
Code
Analysis
Errors:
f[i].second = f[j].second;写成了f[i].second = 1;
f[i].second += f[j].second;写成了f[i].second += 1;
时间复杂度为内外循环O(n^2)
Last updated
Was this helpful?