[leetcode] 300. Longest Increasing Subsequence (Medium)
题意:
求最长增长的子序列的长度。
思路:
利用DP存取以i作为最大点的子序列长度。
Runtime: 20 ms, faster than 35.21% of C++ online submissions for Longest Increasing Subsequence.
class Solution
{
public:
int lengthOfLIS(vector<int> &nums)
{
if (nums.size() == )
return ;
vector<int> dp(nums.size(), );
dp[] = ;
int resNum = ;
for (int i = ; i < nums.size(); i++)
{
int curmaxNum = ; for (int j = ; j < i; j++)
{
if (nums[i] > nums[j])
curmaxNum = max(dp[j], curmaxNum);
}
dp[i] = curmaxNum + ;
resNum = max(dp[i], resNum);
}
return resNum;
}
};
解法二:
讨论区里的最优解:
利用一个容器去动态存储一个增长子序列,遍历Nums,对每一个nums[i],在容器中寻找是否有大于等于nums[i]的元素,若存在,则将改值替换为nums[i];
若不存在,则将nums[i]加入该容器,于是容器中的元素永远是 小于 关系的。
0ms.
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> res;
for(int i=; i<nums.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();
}
};
[leetcode] 300. Longest Increasing Subsequence (Medium)的更多相关文章
- [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 (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
- Leetcode 300 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 ...
- 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
一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 二.题目大意: 给定一个没有排序的数组,要求从该数组中找到一个 ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
随机推荐
- Windows Vista 的历史地位
Windows Vista,这是一个不那么如雷贯耳的Windows名字,很多人甚至从来没有体验过这个操作系统.但是,Windows Vista刚刚推出时候所引起的话题性,恐怕是其后的Win7也难以与之 ...
- Delphi xe5 StyleBook的用法(待续)
首先要在FORM里拖进来一个StyleBook1,然后在Form里设置属性,记住一定要在单击form,在OBject Inspector里设置StyleBook [StyleBook1]. 下一个属 ...
- Linux升级OpenSSL版本
安装nginx的时候,出现了这样的问题: nginx : Depends: libssl1.0.0 (>= 1.0.2~beta3) but 1.0.1f-1ubuntu2.11 is to b ...
- QString unsigned char* 的转换
QString -> unsigned char* : QString str = "ABCD"; int length = str.length(); unsigned ...
- ECMAScript6 语法
声明变量 var let const var(声明局部变量,有局部外可用缺陷) 代码 <!DOCTYPE html> <html lang="en"> &l ...
- python trojan development 3rd —— use python to creative a simple shell
前两篇文章的木马太被动,今天是通过socket和os来进行主动木马编写 有些s13,我真的搞不懂拿一些没过脑子的代码就放到网上去害人,骗流量,还某知名安全企业学院写的,真的服.我的代码自己运行过,很稳 ...
- 基于maven的项目脚手架,一键创建项目的项目模板
制作基于maven的项目脚手架 Springboot的出现极大的简化了项目开发的配置,然而,到真实使用的时候还是会有一堆配置需要设定.比如依赖管理,各种插件,质量扫描配置,docker配置,持续集成配 ...
- PATB 1038. 统计同成绩学生(20)
https://www.patest.cn/contests/pat-b-practise/1038 #include <cstdio> int cnt[110]; int temp[10 ...
- python面试题(三)列表操作
接上一篇............. 0x01:列表的去重操作 al = [1, 1, 2, 3, 1, 2, 4] #set方法元素去重 al_set = set(al) print(list(al_ ...
- mongoshell 执行JavaScript文件获取数据库实例基本信息
由于MongoDB没有关系型数据中强大的数据字典.因此,如果需要汇总统计一些内部信息,包括数据量,基本元信息,集群架构,状态信息.则需要自己写脚本统计.为日后工作方便,本人在此将一些统计信息通过Jav ...