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 ...
随机推荐
- Linux下汇编语言学习笔记43 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- Linux下汇编语言学习笔记13 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- rsync远程文件传输
[root@rhel5 ~]# rsync -a log.tar.gz root@192.168.124.129:/tmp root@192.168.124.129's password: Permi ...
- POJ1328 Radar Installation 解题报告
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- 洛谷——P1062 数列
洛谷——P1062 数列 题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,1 ...
- json数组显示格式
{“colorAndImg”:[{"颜色":“红色”,"地址":“www.sohu.com”}, {“颜色”:“绿色,“地址”:“www.sohu.com”}] ...
- linux复制文件/移动文件从一个文件夹到另一个文件夹
cp ~/baidu/* ~/zhidao/cp -r逐层复制mv /data/link /usr/local 移动文件夹 解决方法 1.在cp命令后,加一个选项 -r . cp -r /TEST/ ...
- Symantec Backup Exec部署手册
转载 http://xiaxiaoguo.blog.51cto.com/858884/402810 Symantec Backup Exec部署手册 目录 1.Backup Exec 12.5安装. ...
- ubuntu下vi的使用
ubuntu下vi的使用 ssh之后对于server的文件,我习惯用gedit,可是不好改动,于是就用vi. 1.vi的基本概念 基本上vi能够分为三种状态,各自是命令模式(command mode) ...
- JSON介绍及Android最全面解析方法(Gson、AS自带org.son、Jackson解析)
前言 今天,我们来介绍一下现今主流的数据交换格式-JSON! 相同作为主流为数据交换格式-XML,假设有兴趣能够阅读我写的XML及其DOM.SAX.PULL解析方法和对照 文件夹 定义 JavaScr ...