Description

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.

my program

思路:创建一个2*n的二维数组,用来记录到当前字符时,最长递增子序列。可以通过两个嵌套循环操作实现统计结果,然后遍历结果找出最大的即为整个数组的最长递增子序列,时间复杂度是O(n2)

class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
if (nums.empty()) return 0;
int res = 1;
vector<vector<int>> ret;
ret.push_back(nums);
ret.push_back(vector<int>(nums.size(), 1));
for (int i = 1; i < nums.size(); i++) {
int max = 1;
for (int j = i - 1; j>=0; j--) {
if (ret[0][j] < ret[0][i] && ret[1][j] >= max) {
max = ret[1][j] + 1;
}
}
ret[1][i] = max;
} for (int i = 1; i < nums.size(); i++) {
if (ret[1][i] > res)
res = ret[1][i];
}
return res;
}
};

Submission Details

24 / 24 test cases passed.

Status: Accepted

Runtime: 29 ms

other

int lengthOfLIS(vector<int>& nums) {
vector<int> res;
for(int i=0; i < nums.size(); i++) {
auto it = std::lower_bound(res.begin(), res.end(), nums[i]);
if(it==res.end()) res.push_back(nums[i]);
else *it = nums[i];
}
return res.size();
}

std::lower_bound:Returns an iterator pointing to the first element in the range [first, last) that is not less than (i.e. greater or equal to) value.

思路:建立最长递增子序列数组。找出当前最长递增子序列数组中第一个大于该数字的,然后替换,如果没有第一个大于该数字的数字,则将其添加到最长递增子序列数组中去。

LeetCode300. Longest Increasing Subsequence的更多相关文章

  1. leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence

    Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...

  2. Leetcode300. Longest Increasing Subsequence最长上升子序列

    给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...

  3. [Swift]LeetCode300. 最长上升子序列 | Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  4. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  5. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  6. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  7. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  8. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  9. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

随机推荐

  1. Mapper动态代理开发

    在开发的过程中只需要写Dao层的借口,无需写其实现类,实现类有框架自己补充. 框架是根据mapper文件自动补充的,因此需要满足下面四个条件 Mapper接口开发需要遵循以下规范: Mapper.xm ...

  2. 【spring data jpa】jpa中使用in查询或删除 在@Query中怎么写 ,报错:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'goodsConfigUid' cannot be found on null 怎么处理

    示例代码如下: @Modifying @Transactional @Query("delete from GoodsBindConfigMapping gbc " + " ...

  3. [转载]iOS6新特征:UICollectionView官方使用示例代码研究

    原文地址:iOS6新特征:UICollectionView官方使用示例代码研究作者:浪友dans 注:这里是iOS6新特征汇总贴链接 iOS6新特征:参考资料和示例汇总 这个链接可以学习到UIColl ...

  4. Bootstrap幻灯片

    Bootstrap幻灯片的制作利用到了Carousel插件,包含:左右箭头.图片.点点导航 <div id="carousel-example-generic" class= ...

  5. Oracle Service Bus中的线程

    jib以前在给客户讲产品的时候经常提到Oracle Service Bus服务总线适合于短连接,高频率的交易,而不适合那些大报文,然后花费很长的调用时间的应用,前一阵在给客户培训完企业服务总线后,又对 ...

  6. http://blog.csdn.net/rosten/article/details/17068285

    http://blog.csdn.net/rosten/article/details/17068285

  7. http://blog.163.com/eugeneheen_chen@126/blog/static/120812157201291994916866/

    http://blog.163.com/eugeneheen_chen@126/blog/static/120812157201291994916866/

  8. Yii2系列教程七:Behaviors And Validations

    这一篇文章的开头就无需多言了,紧接着上一篇的内容和计划,这一篇我们来说说Yii2的Behavior和Validations. Behavior 首先我们来说说Behavior,在Yii2中Behavi ...

  9. 10 Essential TypeScript Tips And Tricks For Angular Devs

    原文: https://www.sitepoint.com/10-essential-typescript-tips-tricks-angular/ ------------------------- ...

  10. C#文本之XML

    格式化XML public static string FormatXML(string XMLstring) { //校验是否是XML报文 if (!XMLstring.Contains(" ...