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 ...
随机推荐
- SpringBoot入门学习(三)
基于第二讲,这一讲我们主要讲解包含以下内容 springBoot添加对freemarker的支持 使用@RestController处理ajax请求 使用@PathVariable注解获取url参数 ...
- iOS “弱账号” 暗转 “强账号”
一.背景 由于某些历史原因,我们产品中50%以上活跃用户是弱账户.即 客户端按照某种规则生成的一个伪id 存在keychain 里,作为这个用户的唯一标识,实现快速登录.正常情况下是不会有问题. 最近 ...
- Linux 模拟网络丢包,延时
实战: 丢包tc qdisc add dev bond0 root netem loss 10% 延迟tc qdisc add dev bond0 root netem latency 100ms 丢 ...
- Log4Net 日志文件分类保存
1.app.config <configSections> <section name="log4net" type="log4net.Config.L ...
- Kafka学习之(四)PHP操作Kafka
简单测试 环境:Centos6.4,PHP7,kafka服务器IP:192.168.9.154,PHP服务器:192.168.9.157 在192.168.9.157创建目录和文件. //生产者 &l ...
- 根据iframe获取window
今天使用layui弹出窗口,需要将函数写在弹出的窗口,但是按钮事件是在父层窗口绑定的,这样就要在父层窗口调用子层窗口的函数. 子层函数与父层函数 function topup() { console. ...
- Vue限制输入框只能输入整数
首先,得明确监听input输入框变化的方法是input,不是change. 方案一:type= "number" 作用: 成功禁止输入字母 能输入小数点,第一位可以为0,小数点能输 ...
- 20145329 《Java程序设计》课程总结
每周读书笔记链接汇总 •第一周读书笔记 http://www.cnblogs.com/jdy1453/p/5248592.html •第二周读书笔记 http://www.cnblogs.com/jd ...
- 重新想,重新看——CSS3变形,过渡与动画④
最后,我们来探讨一下CSS3的动画属性. 之前提到过,实际上过渡也算作动画的一种.但过渡作为动画的缺陷在于,只能使元素属性从一个值“过渡”至另一个值,但如果想要使元素的属性值根据需要在时间轴上不断变化 ...
- 【前端】Vue.js经典开源项目汇总
Vue.js经典开源项目汇总 原文链接:http://www.cnblogs.com/huyong/p/6517949.html Vue是什么? Vue.js(读音 /vjuː/, 类似于 view) ...