[leetcode] 300. Longest Increasing Subsequence
2016-06-29 09:04
435 查看
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?
Solution:
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?
Solution:
int lengthOfLIS(vector<int>& nums) { vector<int> BIS(nums.size() + 1, INT_MAX); int len = 0; for (int i = 0; i < nums.size(); i++) { for (int k = BIS.size() - 1; k >= 0; k--) { if ((nums[i] > BIS[k]) && (nums[i] < BIS[k + 1])) { BIS[k + 1] = nums[i]; } } if (BIS[0] > nums[i]) { BIS[0] = nums[i]; } } for (len = 0; len < BIS.size(); len++) if (BIS[len] >= INT_MAX) break; return len; }
相关文章推荐
- Slow query log日志跟踪
- HBuilder之Chrome调试Android手机
- UITextFiled,UITextView长度限制
- HBuilder之真机运行
- 【Arduino官方教程第一辑】示例程序 6-4 超声波测距传感器
- EasyUI datagrid border处理,加边框,去边框
- Codeforces 163A Substring and Subsequence
- Vue.js——60分钟组件快速入门(上篇)
- 30分钟git命令入门到放弃
- 347. Top K Frequent Elements
- ThreadPoolExecutor原理与使用
- requestWindowFeature无法隐藏标题栏
- Druid
- 优化UITableViewCell高度计算的那些事
- UIScrollView不像样touchBegain点击事件
- iOS8的UIAlertController
- UITextField的leftView和rightView
- SoapUI+Groovy做接口自动化测试
- 更新BLE的设备后,OSX LightBlue 不会更新扫描到Service
- 多线程学习之--真的不能在子线程里更新UI吗?