LeetCode OJ:Longest Increasing Subsequence(最长递增序列)
2015-11-17 20:51
357 查看
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?
dp解决,注意这里的递增序列不是指连续的递增 ,可以是不连续的, 代码如下:
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?
dp解决,注意这里的递增序列不是指连续的递增 ,可以是不连续的, 代码如下:
class Solution { public: int lengthOfLIS(vector<int>& nums) { if(nums.size() <= 1) return nums.size(); vector<int> dp(nums.size(), 0); int maxVal = 1; for(int i = 0; i < nums.size(); ++i){ dp[i] = 1; for(int j = 0; j < i; ++j){ if(nums[j] < nums[i]){ dp[i] = max(dp[i], dp[j]+1); maxVal = max(dp[i], maxVal); } } } return maxVal; } };
相关文章推荐
- Machine Learning week 3 quiz : Regularization
- UVA 11995 - I Can Guess the Data Structure!【stl】
- 在uistoryboard中实现scrollView的添加内容的操作注意事项
- poj 2593Max Sequence【dp】
- mac os slave Slave went offline during the build issue
- My 1st webUI try
- UISegmentedControl 分段控制 一个小demo
- 《iOS Human Interface Guidelines》——Integrating with iOS
- UI 第六天 UISegmentControl UISlider UIImageView
- Frequently Asked Questions in Spark
- UI note1
- UITableView - beginUpdates和endUpdates
- AMD规范与requireJS
- [TwistedFate]滑块UISlider 跑图UIImageView
- UIControl的两个子类 -- UISegmentedControl UISlider
- UI第六天::UIControl及其⼦类
- 【hihocoder1255 2015北京赛区G】【简单模拟 简化写法超棒哦】 Mysterious Antiques in Sackler Museum 四个矩形选三个 恰好拼成大矩形
- Machine Learning week 3 quiz : Logistic Regression
- ios开发系列-UITableController-索引
- How Did Watson Answer —— Automatic Knowledge Acquisition