LeetCode OJ:Longest Increasing Subsequence(最长递增序列)
2024-09-05 07:41:08
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?
dp解决,注意这里的递增序列不是指连续的递增 ,可以是不连续的, 代码如下:
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
if(nums.size() <= ) return nums.size();
vector<int> dp(nums.size(), );
int maxVal = ;
for(int i = ; i < nums.size(); ++i){
dp[i] = ;
for(int j = ; j < i; ++j){
if(nums[j] < nums[i]){
dp[i] = max(dp[i], dp[j]+);
maxVal = max(dp[i], maxVal);
}
}
}
return maxVal;
}
};
最新文章
- SVN版本库(访问权限)配置实例笔记
- Mysql 连接sleep状态问题解决。
- php数组函数,字符串,linux命令
- ActiveMQ第五弹:增加ReDelivery功能
- [Angular2 Router] Understand the Angular 2 Base href Requirement
- AMQ学习笔记 - 06. 可靠消息传送
- [JQuery]学习总结
- CentOS6安装配置rsh
- Unity问答——请问一下动画状态机怎么判断动画是否播完了?
- Git应用于Android项目的入门知识:我的理解
- Sending HTML Form Data
- kettle工具实现报表导出的初步搭建
- fopen fclose feof fgets fetl
- java.lang.UnsupportedClassVersionError: JVMCFRE003解决方法--jdk 1.6 中switch的参数无法使用String类型
- GoldenGate OGG-01032 There Is a Problem in Network Communication Error in Writing to Rmt Remote Trail Rmttrail (Doc ID 1446621.1)
- Java之旅_高级教_集合框架
- 谈论linux同组多用户操作问题
- 【Java】快速排序的非递归实现
- android 横竖屏 切换
- 微信小程序 - 自定义switch切换(示例)