LeetCode OJ:Longest Increasing Subsequence(最长递增序列)
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;
}
};
LeetCode OJ:Longest Increasing Subsequence(最长递增序列)的更多相关文章
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [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 ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- POJ 2533 Longest Ordered Subsequence 最长递增序列
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
随机推荐
- springer论文模板参考文献的顺序问题
latex环境 MikTex 2.9 + TeXstudio 2.12.8 (+ Mendeley) 问题 springer提供的latex模板 中最后的参考文献是按照字母顺序排列的.我想要弄成按照文 ...
- java synchronized关键字的底层实现
每个对象都有一个锁(Monitor,监视器锁),class对象也有锁,如果synchronized关键字修饰同步代码块,通过反编译可以看到,其实是有个monitorenter和monitorexit指 ...
- javascript 基本数据类型、引用数据类型
阅读目录 数据类型 两种访问方式 两种类型复制 函数参数的传递 两种变量类型检测 回到目录 数据类型 1. ECMAScript变量包含两种不同类型的值:基本类型值.引用类型值: 2. 基 ...
- Ubuntu16.04安装Jenkins
Jenkins基于JAVA,所以需要先安装jdk 安装java 在官网上下载jdk,http://www.oracle.com/technetwork/java/javase/downloads/jd ...
- Python --之练习题
一,两个小组对战,对战规则如下:team1 = ['a','b','c']team2 = ['x','y','z'] #a 不和x对战,b 不和y,z 对战# for i in team1: #法一# ...
- .NET BETWEEN方法
Between 值范围比较 可以判断一个值是否落在区间范围值中. public static bool Between<T>(this T me, T lower, T upper) wh ...
- Redis学习笔记之Redis单机,伪集群,Sentinel主从复制的安装和配置
0x00 Redis简介 Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure server). Redis的键值 ...
- 20145302张薇《Java程序设计》第七周学习总结
20145302 <Java程序设计>第七周学习总结 教材学习内容总结 第十三章 时间的度量 Greenwich Mean Time,格林威治时间,简称GMT时间,由观察太阳而得来: Un ...
- AD9361
AD9361框图 1. Fir滤波器的阶数为64或128 而内插或抽取因子为:1.2或4. HB1和HB2的内插或抽取因子为1或2而HB3的因子为1.2或3 BB_LPF为:三阶巴特沃斯低通滤波器 ...
- Android系统启动过程[典☆☆☆]
Android系统启动过程 首先Android框架架构图:(来自网上,我觉得这张图看起来很清晰) Linux内核启动之后就到Android Init进程,进而启动Android相关的服务和应用. 启动 ...