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的更多相关文章

  1. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

  2. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  3. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  4. 673. Number of Longest Increasing Subsequence最长递增子序列的数量

    [抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...

  5. POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

    题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...

  6. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  7. [leetcode]300. Longest Increasing Subsequence最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  8. 最长递增子序列 (LIS) Longest Increasing Subsequence

    问题描述: 有一个长为n的数列a0, a1,..., an-1.请求出这个序列中最长的上升子序列.请求出这个序列中最长的上升子序列. 上升子序列:对于任意i<j都满足ai<aj的子序列. ...

  9. poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)

    两种算法 1.  O(n^2) #include<iostream> #include<cstdio> #include<cstring> using namesp ...

随机推荐

  1. JMM和底层实现原理

  2. C#对Windows服务组的启动与停止

    Windows服务大家都不陌生,Windows服务组的概念,貌似MS并没有这个说法. 作为一名软件开发者,我们的机器上安装有各种开发工具,伴随着各种相关服务. Visual Studio可以不打开,S ...

  3. 01.css选择器-->类选择器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. js 随机打乱数组

    假如有一个数组: var arr1=['a','b','c','d','e','f','g']; 需要将它进行随机打乱,网上好多都是用: arr1.sort(function(){ return 0. ...

  5. 推荐js库: underscore

    Underscore封装了常用的JavaScript对象操作方法,用于提高开发效率. 之间没用他之前,自己写,那是相当的酸爽. 如循环处理: for(var i=0;i<data.length; ...

  6. jpa dialect设置

    1.如果配置文件格式为application.properties,在配置文件中添加以下代码即可: spring.jpa.database-platform=org.hibernate.dialect ...

  7. MySQL 常用语句大全

    MySQL 常用语句大全 一.连接 MySQL 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 1.例 1:连接到本机上的 MYSQL. 首先在打开 DOS 窗口,然后进入目录 my ...

  8. Appium元素定位(uiautomatorviewer)

    一.uiautomatorviewer元素定位 1.adroid-sdk的安装目录tools下有1个自带的工具uiautomatorviewer,打开后,如下所示: 点击后,如图所示: 步骤: a.链 ...

  9. centos安装flash

    自己操作步骤: 1  :http://get.adobe.com/cn/flashplayer/ 还是进入此下载页选择“.rpm,适用于其它Linux”,下载该rpm文件 2   :# rpm -iv ...

  10. spring boot(5)-properties参数配置

     application.properties application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件 s ...