[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
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/
[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数的更多相关文章
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- LeetCode Number of Longest Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- POJ 2533 Longest Ordered Subsequence 最长递增序列
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
随机推荐
- java中的并发工具类
在jdk的并发包里提供了几个非常有用的并发工具类.CountDownLatdch.CyclicBarrier和Semaphore工具类提供了一种并发流程控制的手段,Exchanger工具类则提供了在线 ...
- freemarker 类型转换
操作字符串函数 1. substring(start,end)从一个字符串中截取子串 start:截取子串开始的索引,start必须大于等于0,小于等于endend: 截取子串的长度,end必须大 ...
- Oracle profile 使用技巧
给scott用户分配一个profile要求如下: 1.尝试登录的次数最多4次: 2.如果4次输入错误,则锁定该用户2天: 3.密码每隔5天修改一次,宽限期为2天: 答: SQL>conn sys ...
- 2018最新版本Sublime Text3注册码(仅供测试交流使用)
-– BEGIN LICENSE -– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...
- 系统右键添加cmd命令
我们运行cmd.exe时,会发现刚刚打开时,一般提示在默认路径下: 有时候我们希望直接能够切换到某个路径下运行程序或者做某些工作,那么切换路径可能就会比较麻烦.下面我们介绍一种比较实用的方法,用鼠标右 ...
- 网络推广 免费推广产品网站 B2B网站如何推广
云集网(yunjinet.com)免费发布各类服务和产品信息,在平台上推广你的产品.帮助商家推广优质的产品和服务.如何提高信息的点击量为了提高分类信息网的信息质量,对重复度高.相似度高的信息进行了过滤 ...
- 记录python接口自动化测试--主函数(第六目)
把操作excel的方法封装好后,就可以用准备好的接口用例来循环遍历了 我的接口测试用例如下 主函数代码: run_handle_excel.py# coding:utf-8 from base.run ...
- C语言第四次博客作业
一.PTA实验作业 题目1.梅森数 1.本题PTA提交列表(要提交列表,不是结果) 2. 设计思路(此处用流程图最好) 1.定义三个变量n,num,count,且初始化count为1 2.读取一个数n ...
- 201621123062《java程序设计》第十周作业总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 思维导图: 2. 书面作业 本次PTA作业题集异常 2.1. 常用异常 结合题集题目7-1回答 2.1.1 自己以前 ...
- Beta预备
团队名称:稳住!我们能赢 Beta预备: 讨论组长是否重选的议题和结论 项目组长可以说是一个团队的灵魂和核心.一个好的领导者可以激发团队成员的工作热情,提高开发效率,保质保量的完成工作.虽然在Alph ...