[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 ...
随机推荐
- linux下编译qt5.6.0静态库(使用./configure --help来看看都有哪些参数。超详细,有每一个模块的说明。如果改变了安装的目录,需要到安装目录下的bin目录下创建文件qt.conf)(乌合之众)good
linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...
- Ubuntu14.04 静态编译安装Qt4.8.6
./configure -static -nomake demos -nomake examples -nomake tools -no-exceptions -prefix /usr/local/Q ...
- Oracle数据库密码重置、导入导出库命令
重置办法如下:打开CMD命令提示符,然后输入下面命令进行重置: 输入sqlplus /nolog,回车 SQL> conn /as sysdba 已连接: SQL>alter user s ...
- 事务 ( 进程 ID 60) 与另一个进程被死锁在锁资源上,并且已被选作死锁牺牲品
Select * FROM [TableName] With(NoLock) .....
- Ubuntu --- Virtualbox 和 宿主机文件夹共享
1.在设置里面共享文件夹 2.在Ubuntu中配置 sudo mount -t vboxsf share /var/www/html/ 无需重启即可生效 3.实现系统重启后也自动挂载 在文件 /etc ...
- vmware vSphere Data Protection 6.1 使用备份、恢复、报告
一.6个选项卡说明 1.getting started 开始,提供VDP功能概述以及指向创建备份作业向导.恢复向导.报告选项卡的快速连接 2.backup 提供已计划备份作业的列表以及有关备份作业的详 ...
- 智能小程序关于Filter过滤器的简单使用
<filter module="swan"> export default { imgurl: (imgUrl) => { var imgurlprefix = ...
- idea初见问题整理_错误: -source 1.5 中不支持 diamond 运算符
最近在移动工程到idea下,顺便改目录结构,遇到的问题不一定全部记录,有些答案摘抄自别人博客,已注明来源,由于不是摘抄自同一作者,且有自己的一些内容,所以标注为原创. 1.(错误: -source 1 ...
- shell遍历文件
取文件每行的数据,需要按列取 可以 sed 加管道 使用 awk 取列 platform="list.txt" line=`grep -vc '^$' $platform` ; ...
- fis3前端工程构建配置入门教程
一.前言 fis3是百度推出的一款前端工程构建工具,类似的还有webpack,gulp等工具:无论大家有没有使用过,从事前端行业应该都略知一二了,所以对于此类工具用干嘛的我这里就不做重复了. 其实对于 ...