LintCode 76 最长上升序列
来源:互联网 发布:数控车床锥度螺纹编程 编辑:程序博客网 时间:2024/06/05 19:53
题目:longestIncreasingSubsequence
要求:
给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度。
样例:
给出 [5,4,1,2,3],LIS 是 [1,2,3],返回 3
给出 [4,2,4,5,3,7],LIS 是 [2,4,5,7],返回 4
算法要求:
要求时间复杂度为O(n^2) 或者 O(nlogn)
解题思路:
用dp数组存当前最长的长度。
算法如下:
int longestIncreasingSubsequence(vector<int> nums) { // write your code here if (nums.size() == 0) { return 0; } int dp[1001] = {0}; int ans; dp[1] = 1; ans = 1; int size = nums.size(); for(int i = 0; i < size; i++) { int m = 0; for(int j = 0; j < i; j++) { if(dp[j] > m && nums[j] < nums[i]) { m = dp[j]; } } dp[i] = m + 1; if(dp[i] > ans) { ans = dp[i]; } } return ans; }
阅读全文
0 0
- LintCode 76 最长上升序列
- lintcode-最长上升子序列-76
- lintcode(76)最长上升子序列
- LintCode:最长上升子序列
- LintCode 最长上升子序列
- 最长上升子序列-LintCode
- lintcode--最长上升子序列
- LintCode--最长上升连续子序列
- lintcode-最长上升连续子序列-397
- LintCode -- 最长上升子序列 O(nlgn)
- 最长上升连续子序列(LintCode)
- LintCode- 最长上升连续子序列
- lintCode刷题--最长上升连续子序列
- LintCode:最长上升连续子序列
- LintCode 77.最长上升子序列
- LintCode(M)最长上升子序列
- lintcode -- 最长上升连续子序列
- lintcode:最长上升连续子序列
- Spring 源码粘贴1 IoC
- kali中VMtools的安装
- 关于弗洛伊德算法
- docker --help 命令
- Node.js安装及环境配置之Windows篇
- LintCode 76 最长上升序列
- Vue---- 简单的图书管理
- Java ConcurrentModificationException when clearing List and ArrayList
- 中了勒索病毒要怎么办
- JavaWeb实战(四)——加入MyBatis框架
- 【解决】网站运行一段时间后就无法访问,重启Tomcat才能恢复
- laravel 定时任务
- 简历弄得这么“丑”,HR不会给面试的!
- Redis集群生产环境高可用方案实战过程