LeetCode *** 300. Longest Increasing Subsequence
2016-04-13 22:21
441 查看
题目:
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given
The longest increasing subsequence is
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
分析:
我目前做的是n^2时间复杂度。有时间了想n*logn复杂度的做法吧。占坑。
代码:
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?
分析:
我目前做的是n^2时间复杂度。有时间了想n*logn复杂度的做法吧。占坑。
代码:
class Solution { public: int lengthOfLIS(vector<int>& nums) { int max=0,size=nums.size(); for(int i=0;i<size;++i){ int tmp=1,pre=nums[i],next=nums[i]; for(int j=i-1;j>=0;--j){ if(nums[j]<pre){ tmp++; pre=nums[j]; } } for(int j=i+1;j<size;++j){ if(nums[j]>next){ tmp++; next=nums[j]; } } max=max>tmp?max:tmp; } return max; } };
相关文章推荐
- iOS限定UITextField的输入格式
- (4)Java设计模式-建造者模式(Builder)
- 说说 UINavigationBar 中的 translucent 属性
- hdu5412 CRB and Queries (整体二分)
- IOS UIPanGestureRecognizer 移动图片
- STL常用容器总结——deque双向队列
- Sequence one
- Sequence one
- Rescue The Princess
- 基于 SoapUI 工具测试 Rest 服务
- 设计模式之建造者模式(Builder)
- 制作网页---BJUI订制控件
- UIView的常用属性(如clipsTobounds)
- HDUOJ 1005 Number Sequence(DP求公式)
- 通过UIColor创建图片
- Android UI-实现底部切换标签(fragment)
- lightoj 1369 - Answering Queries 数学
- WH服务器框架分析系列一:内核模块的基石CQueueService
- UIImagePickerController以中文显示界面
- iOS UITableView 引起的崩溃问题