300. Longest Increasing Subsequence
2016-04-22 00:03
330 查看
Given an unsorted array of integers, find the length of longest increasing subsequence.
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?
Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.
最长上升子序列,N^2的算法,比较简单,简单的动态规划思想。
不过nlogn的算法,之前还真不知道,今天查了下大神的代码,学习了!
主要思路就是用一个数组记录长度len为1~i的递增序列的最后一个元素的值。
查找的时候利用二分的思路。
/article/9565797.html
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?
Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.
最长上升子序列,N^2的算法,比较简单,简单的动态规划思想。
class Solution { public: int lengthOfLIS(vector<int>& nums) { int len=nums.size(); if(len==0)return 0; vector<int>f(len); int maxx=1; for(int i=0;i<len;i++) f[i]=1; for(int i=1;i<len;i++){ for(int j=0;j<i;j++){ if(nums[i]>nums[j]&&f[j]+1>f[i]){ f[i]=f[j]+1; } maxx=max(maxx,f[i]); } } return maxx; } };
不过nlogn的算法,之前还真不知道,今天查了下大神的代码,学习了!
主要思路就是用一个数组记录长度len为1~i的递增序列的最后一个元素的值。
查找的时候利用二分的思路。
/article/9565797.html
相关文章推荐
- Do not request Window.FEATURE_SUPPORT_ACTION_BAR
- 关于WPF的Button 的click事件调用 code UI 代码的示例(有益于快速的自动化)
- data-model 简单介绍
- 简单日历封装
- Deque 容器
- Queue容器
- Xcode中buildSetting参数设置与学习
- 在iOS 8中使用UIAlertController
- mysql_query返回false原因
- Java基础-break,continue,return
- iOS开发系列--UITableView全面解析
- UE4:编辑器中显示物体包围框
- 十六进制string转换UIColor -备用
- HDU - 1159 Common Subsequence
- burpsuite中的几款插件推荐
- UVA_10534_Wavio Sequence
- This warning can be disabled by using the command line flag -Dcom.android.build.gradle.overridePath
- Xcode UUID查询
- ACE_Message_Queue和spawn实现(生产者/消费者)(V2.00)
- 南京理工大学第八届程序设计大赛(校外镜像) J sequence