300.LeetCode Longest Increasing Subsequence(medium)[动态规划]
2016-03-29 21:47
549 查看
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given
The longest increasing subsequence is
Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
这里要求最长增长子序列,那么采用动态规划的思想,对于每个元素位置的最长增长子序列是该元素之前的比当前元素小的最大增长子序列的长度加上1.
For example,
Given
[10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is
[2, 3, 7, 101], therefore the length is
4.
Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
这里要求最长增长子序列,那么采用动态规划的思想,对于每个元素位置的最长增长子序列是该元素之前的比当前元素小的最大增长子序列的长度加上1.
class Solution { public: int lengthOfLIS(vector<int>& nums) { if(nums.size()<=1) return nums.size(); int n = nums.size(); vector<int> dp(n); dp[0] = 1; for(int i=1;i<n;i++) { int max = 0; for(int j=0;j<i;j++) { if(nums[j]<nums[i]) { if(max<dp[j]) max = dp[j]; } } dp[i] = max+1; } sort(dp.begin(),dp.end()); return dp[n-1]; } };
相关文章推荐
- 问题easyui树形成了死循环
- 0329 复利计算器5.0 Juint单元测试 组员 254列志华 253韩麒麟
- JavaGUI设计
- 128.LeetCode Longest Consecutive Sequence(hard)[数组 排序 最长连续增长子序列]
- dll signing issue
- 简单的电影介绍应用
- 使用 CPUID 查询 CPU 信息
- OpenStack虚机迁移live-migration失败(error: internal error Attempt to migrate guest to the same host) 推荐
- 根据label字数计算UILable高度
- CI Query Builder
- hdu-2818/POJ-1988 Building Block(并查集+路径压缩)
- IOS UI知识点收集
- Building a Space Station(POJ_2031)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- [POJ2778]DNA Sequence
- IOS开发实践篇--UITableview嵌套UITableView案例实践(仿淘宝商品详情页实现)
- 自定义UITableView的左滑动样式
- UIView生命周期(控制器中函数的执行顺序)
- BurpSuite工具应用
- iOS UIButton左字右图 + 选择