标签:tco turn 结果 solution 序列 size 一个 并且 vector
给定一个未排序的整数数组,找到最长递增子序列的个数。
示例 1:
输入: [1,3,5,4,7] 输出: 2 解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。
示例 2:
输入: [2,2,2,2,2] 输出: 5 解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。
注意: 给定的数组长度不超过 2000 并且结果一定是32位有符号整数。
class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
if(nums.size()<2)
{
return nums.size();
}
vector<pair<int,int>> dp(nums.size(),{1,1});
int maxlength = 1;
int cnt = 1;
for(int i = 1;i < nums.size();++i)
{
if(dp[i].first == maxlength)
{
cnt += dp[i].second;
}
for(int j = 0;j < i;j++)
{
if(nums[i] > nums[j])
{
if(dp[j].first + 1 > maxlength)
{
maxlength = dp[j].first + 1;
cnt = dp[j].second;
}
else if(dp[j].first + 1 == maxlength)
{
cnt += dp[j].second;
}
if(dp[j].first + 1 > dp[i].first)
{
dp[i].first = dp[j].first + 1;
dp[i].second = dp[j].second;
}
else if(dp[j].first + 1 == dp[i].first)
{
dp[i].second += dp[j].second;
}
}
}
}
return cnt;
}
};
标签:tco turn 结果 solution 序列 size 一个 并且 vector
原文地址:https://www.cnblogs.com/Jawen/p/10827352.html