leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列
子序列不是数组中连续的数。
dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列。
初始化是dp所有的都为1,最终的结果是求dp所有的数值的最大值。
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int length = nums.size();
if(length <= )
return ;
vector<int> dp(length,);
int max_num;
for(int i = ;i < length;i++){
max_num = ;
for(int j = i - ;j >= ;j--){
if(nums[i] > nums[j])
max_num = max(max_num,dp[j] + );
}
dp[i] = max_num;
}
max_num = ;
for(int i = ;i < length;i++){
if(dp[i] > max_num)
max_num = dp[i];
}
return max_num;
}
};
674. Longest Continuous Increasing Subsequence
相对于最长递增子序列来说,这个题目更加简单,只需要比较前一个数字就好,不用把前面的数字都比较完。因为如果比前一个小,直接就无法完成递增,只能保持当前的值1;如果比前一个数字大,其实前一个数字就已经计算了之前递增的个数,直接加1就好了
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
if(nums.empty())
return ;
int length = nums.size();
vector<int> dp(length,);
for(int i = ;i < length;i++){
if(nums[i] > nums[i-])
dp[i] = dp[i-] + ;
}
int max_num = ;
for(int i = ;i < length;i++)
max_num = max(max_num,dp[i]);
return max_num;
}
};
leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence的更多相关文章
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]
题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- 最长递增子序列 (LIS) Longest Increasing Subsequence
问题描述: 有一个长为n的数列a0, a1,..., an-1.请求出这个序列中最长的上升子序列.请求出这个序列中最长的上升子序列. 上升子序列:对于任意i<j都满足ai<aj的子序列. ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)
两种算法 1. O(n^2) #include<iostream> #include<cstdio> #include<cstring> using namesp ...
随机推荐
- uestc 1709 Binary Operations 位运算的灵活运用
Binary Operations Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674 Description B ...
- spss C# 二次开发 学习笔记(三)——Spss .Net 开发
Spss .Net 二次开发的学习过程暂停了一段时间,今天开始重启. 之前脑残的不得了,本想从网上下载一个Spss的安装包,然后安装学习.于是百度搜索Spss,在百度搜索框的列表中看到Spss17.S ...
- 关于二进制和字符串及base64格式
字符串转二进制可以直接转,而二进制转字符串不可以,其中间包含的各种特殊符号,转成字符串时会出现问题,需要将二进制进行base64编码,并且需要在结尾加上#0表示结尾,然后转成字符串.
- Luogu2483 [SDOI2010]魔法猪学院(可并堆)
俞鼎力大牛的课件 对于原图以 \(t\) 为根建出任意一棵最短路径树 \(T\),即反着从 \(t\) 跑出到所有点的最短路 \(dis\) 它有一些性质: 性质1: 对于一条 \(s\) 到 \(t ...
- lnmp环境 开启pathinfo
thinkphp url访问模式中 默认的pathinfo不起作用? 1.检查你的tp配置文件config.php URL模式 'url_model'=> '1', //URL模式 即pathi ...
- localStorage跟cookie的使用
最近做了记住密码功能,用localStorage跟cookie都尝试用了一下,感觉都挺好哈,很方便,特此记录 html代码: <input type="text" id=&q ...
- Python 关于bytes类方法对数字转换的误区, Json的重要性
本文起源于一次犯错, 在发觉bytes()里面可以填数字, 转出来的也是bytes类型, 就心急把里面的东西decode出来. 结果为空.搞来搞去以为是命令不熟练事实上错在逻辑. a1 = bytes ...
- visual studio 2015通过附加进程调试wcf服务
网站: 打开wcf服务所在的项目 然后调用iis上部署的HLFC(crm)项目的接口就可以进行调试 注意 WCF服务项目要以管理员身份打开,调用wcf服务的项目要在iis中部署并点击调用后才能在附加到 ...
- SSM Controller 页面之间跳转 重定向,有参 无参问题
需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. (1)我在后台一个controller跳转 ...
- RequestMapping 支持的方法
基于版本:Spring Framework 4.3.5.RELEASE 1.支持指定多个方法 @RequestMapping(value = "/index", method = ...