300 Longest Increasing Subsequence 最长上升子序列
给出一个无序的整形数组,找到最长上升子序列的长度。
例如,
给出 [10, 9, 2, 5, 3, 7, 101, 18],
最长的上升子序列是 [2, 3, 7, 101],因此它的长度是4。因为可能会有超过一种的最长上升子序列的组合,因此你只需要输出对应的长度即可。
你的算法的时间复杂度应该在 O(n2) 之内。
进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗?
详见:https://leetcode.com/problems/longest-increasing-subsequence/description/
Java实现:
class Solution {
public int lengthOfLIS(int[] nums) {
int n=nums.length;
if(n==0||nums==null){
return 0;
}
int[] dp=new int[n];
Arrays.fill(dp,1);
int res=1;
for(int i=1;i<n;++i){
for(int j=0;j<i;++j){
if(nums[j]<nums[i]&&dp[j]+1>dp[i]){
dp[i]=dp[j]+1;
}
if(res<dp[i]){
res=dp[i];
}
}
}
return res;
}
}
C++实现:
方法一:
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int size=nums.size();
if(size==0||nums.empty())
{
return 0;
}
vector<int> maxLen(size,1);
int res=1;
for(int i=1;i<size;++i)
{
for(int j=0;j<i;++j)
{
if(nums[j]<nums[i]&&maxLen[j]+1>maxLen[i])
{
maxLen[i]=maxLen[j]+1;
}
if(res<maxLen[i])
{
res=maxLen[i];
}
}
}
return res;
}
};
方法二:
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int size=nums.size();
if(size==0||nums.empty())
{
return 0;
}
vector<int> res;
for(int i=0;i<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();
}
};
参考:https://www.cnblogs.com/grandyang/p/4938187.html
300 Longest Increasing Subsequence 最长上升子序列的更多相关文章
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- Leetcode300. Longest Increasing Subsequence最长上升子序列
给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
随机推荐
- Python基础之 一
语言分类:编译型(运行前先编译)和解释型(直接运行),静态语言(需要声明变量类型)和动态语言(不需要声明),强类型定义语言(定义好类型不做强制转换就不可修改类型)和弱类型定义语言(数据类型可以被忽略) ...
- 洛谷——P1151 子数整数
P1151 子数整数 题目描述 对于一个五位数a1a2a3a4a5,可将其拆分为三个子数: sub1=a1a2a3 sub2=a2a3a4 sub3=a3a4a5 例如,五位数20207可以拆分成 s ...
- 将mysql数据库数据导出为.sql文件
打开Navicat ,在我们要到处的数据上面右击鼠标,然后弹出的快捷菜单上点击“转储SQL 文件”,在再次弹出的子菜单项中选择第一个“数据跟结构”.
- ArcGIS Engine效率探究——要素的添加和删除、属性的读取和更新
ArcGIS Engine效率探究——要素的添加和删除.属性的读取和更新 来自:http://blog.csdn.net/freewaywalker/article/details/23703863 ...
- Ansible 2.0公布
本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2016/02/ansible-2-released 经过了一年的开发工作后,Ansib ...
- [React] Make Compound React Components Flexible
Our current compound component implementation is great, but it's limited in that users cannot render ...
- react 组件之间的通信
react推崇的是单向数据流,自上而下进行数据的传递,但是由下而上或者不在一条数据流上的组件之间的通信就会变的复杂.解决通信问题的方法很多,如果只是父子级关系,父级可以将一个回调函数当作属性传递给子级 ...
- 理解C语言中指针的声明以及复杂声明的语法
昨天刚把<C程序设计语言>中"指针与数组"章节读完,最终把心中的疑惑彻底解开了.如今记录下我对指针声明的理解.顺便说下怎样在C语言中创建复杂声明以及读懂复杂声明. 本文 ...
- Akka并发编程——第五节:Actor模型(四)
本节主要内容: 1. 停止Actor 1. 停止Actor (1)通过ActorSystem.shutdown方法停止全部 Actor的执行 /* *停止Actor:ActorSystem.shutd ...
- C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释
RTTI(RunTime Type Information)执行时类型信息 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details ...