673. Number of Longest Increasing Subsequence 最长递增子序列的个数
来源:互联网 发布:去学校卖软件 编辑:程序博客网 时间:2024/06/06 01:00
Given an unsorted array of integers, find the number of longest increasing subsequence.
Example 1:
Input: [1,3,5,4,7]Output: 2Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input: [2,2,2,2,2]Output: 5Explanation: 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.
我的思路:是不对的。
class Solution {public: struct comp{ bool operator()(const pair<int,int>& a,const pair<int,int>& b){ return a.first<b.first; } }; //这种代码是错误的,因为重复 int findNumberOfLIS(vector<int>& nums) { //这道题不能使用最长递增子序列长度的二分方法,因为那种方法只保存了每个长度结尾的最小值,不含有相同长度的子序列统计值 //但是我们可以尝试把那个存储结尾最小值的数组改成一个pair,first保存当前的最小值,如果出现了替换,就增大second值。 //时间复杂度应该是nlog(n)的,使用lower_bound的自定义比较函数 vector<pair<int,int>> res; for(int i=0;i<nums.size();i++) { pair<int,int> temp=make_pair(nums[i],0); auto it=lower_bound(res.begin(),res.end(),temp,comp()); if(it==res.end()) res.push_back(make_pair(nums[i],1)); else { (*it).first=nums[i]; (*it).second++; } } return res.size()?res[res.size()-1].second:0; }};第二种错误解法:res存储的是当前长度的字串的数量。这个统计是不完整的
class Solution {public: int findNumberOfLIS(vector<int>& nums) { vector<int> dp(nums.size(),1);//以当前字符结尾的长度 vector<int> res(nums.size()+1,0); res[1]=nums.size();//长度为1的字串有n个 for(int i=1;i<nums.size();i++) { for(int j=i-1;j>=0;j--) if(nums[i]>nums[j]) { int temp=dp[j]+1; res[temp]++; dp[i]=max(dp[i],temp); } } int r=0; for(int i=nums.size();i>=0;i--) if(res[i]>0) { r=res[i]; break; } return r; }};正确答案:
class Solution {public: int findNumberOfLIS(vector<int>& nums) { vector<int> dp(nums.size(),1);//以当前字符结尾的最大长度 vector<int> res(nums.size(),1);//以当前字符结尾的最大长度的个数 int maxLen=1; for(int i=1;i<nums.size();i++) { for(int j=0;j<i;j++) { if(nums[i]>nums[j]&&dp[j]+1>dp[i]) { dp[i]=dp[j]+1; res[i]=res[j]; } else if(nums[i]>nums[j]&&dp[j]+1==dp[i]) res[i]+=res[j]; } maxLen=max(maxLen,dp[i]); } int n=0; for(int i=0;i<nums.size();i++) if(dp[i]==maxLen) n+=res[i]; return n; }};
阅读全文
0 0
- 673. Number of Longest Increasing Subsequence 最长递增子序列的个数
- 最长递增子序列 (Longest Increasing Subsequence)
- 最长递增子序列 (Longest Increasing Subsequence)
- 最长递增子序列 (Longest Increasing Subsequence)
- 最长递增子序列 (Longest Increasing Subsequence)
- Longest increasing subsequence,最长递增子序列
- 最长递增子序列 (Longest Increasing Subsequence)
- 最长递增子序列-Longest Increasing Subsequence
- 最长递增子序列 (Longest Increasing Subsequence)
- Longest Increasing Subsequence 最长递增子序列
- [LeetCode] Longest Increasing Subsequence 最长递增子序列的长度
- 最长递增子序列详解(longest increasing subsequence)
- 最长递增子序列详解(longest increasing subsequence)
- 最长递增子序列详解(longest increasing subsequence)
- longest increasing subsequence (最长递增子序列)
- [动态规划-1] 最长递增子序列-Longest Increasing Subsequence
- [动态规划] 最长递增子序列 (Longest Increasing Subsequence)
- 最长递增子序列详解(longest increasing subsequence)
- 写一个函数求出一个字符串中所有字母的出现次数,不区分大小写
- 总结前端面试过程中最容易出现的问题
- Mac安装office2016以及破解
- oracle listagg函数、lag函数、lead函数 实例
- 移动端总结和手机兼容问题
- 673. Number of Longest Increasing Subsequence 最长递增子序列的个数
- spring事务传播
- Thinkphp 3.2 下的auth权限认证
- Android的taskAffinity对四种launchMode的影响
- jquery操作button为禁用和隐藏
- Mac之安装软件 报错"app"已损坏,打不开。您应该将它移到废纸篓
- C语言实现数据结构之栈的详解
- Leetcode OJ 129 Sum Root to Leaf Numbers [Medium]
- 将逗号分隔的字符串转换为List以及List转逗号分隔的字符串