LeetCode300. Longest Increasing Subsequence
Description
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.
my program
思路:创建一个2*n的二维数组,用来记录到当前字符时,最长递增子序列。可以通过两个嵌套循环操作实现统计结果,然后遍历结果找出最大的即为整个数组的最长递增子序列,时间复杂度是O(n2)
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
if (nums.empty()) return 0;
int res = 1;
vector<vector<int>> ret;
ret.push_back(nums);
ret.push_back(vector<int>(nums.size(), 1));
for (int i = 1; i < nums.size(); i++) {
int max = 1;
for (int j = i - 1; j>=0; j--) {
if (ret[0][j] < ret[0][i] && ret[1][j] >= max) {
max = ret[1][j] + 1;
}
}
ret[1][i] = max;
}
for (int i = 1; i < nums.size(); i++) {
if (ret[1][i] > res)
res = ret[1][i];
}
return res;
}
};
Submission Details
24 / 24 test cases passed.
Status: Accepted
Runtime: 29 ms
other
int lengthOfLIS(vector<int>& nums) {
vector<int> res;
for(int i=0; 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();
}
std::lower_bound:Returns an iterator pointing to the first element in the range [first, last) that is not less than (i.e. greater or equal to) value.
思路:建立最长递增子序列数组。找出当前最长递增子序列数组中第一个大于该数字的,然后替换,如果没有第一个大于该数字的数字,则将其添加到最长递增子序列数组中去。
LeetCode300. Longest Increasing Subsequence的更多相关文章
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- Leetcode300. Longest Increasing Subsequence最长上升子序列
给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...
- [Swift]LeetCode300. 最长上升子序列 | Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [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. For example, ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
随机推荐
- 使用DMV调优性能 --Burgess_Liu
http://blog.csdn.net/burgess_liu/article/details/52813727
- vim 代码注释插件
参考: 1.http://www.vim.org/scripts/script.php?script_id=1218 2.http://www.dutor.net/index.php/2010/05/ ...
- win10下安装mysql5.6 zip形式步骤
1. 解压之后可以将该文件夹改名,放到合适的位置,个人建议把文件夹改名为MySQL Server 5.6,放到C:\Program Files\MySQL路径中. 2. 添加环境变量.path中添加C ...
- mormot数据库连接+查询+序列为JSON
mormot数据库连接+查询+序列为JSON uses SynDB,SynCommons, SynDBRemote, SynOleDB, SynDBMidasVCL, mORMotMidasVCL p ...
- 【SQL Server】sql server更改了数据表的字段/新增数据表的字段 无法保存
sql server更改了数据表的字段/新增数据表的字段 无法保存 解决方法:进入 工具-->选项-->Designers-->表设计器和数据库设计器-->取消勾选 即可
- Docker时间和宿主同步
通过date命令查看时间 查看主机时间 [root@localhost ~]# date 2016年 07月 27日 星期三 22:42:44 CST 查看容器时间 root@b43340ecf5ef ...
- iOS:APNS推送主要代码
首先,在AppDelegate.m 中: 1,注册通知 //[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片 - (BOOL)application:(UI ...
- knowledgeroot 配置
首先下载 KnowledgeRoot 的安装包,就是一个压缩文件,解压缩后放到 WebRoot 下面 在浏览器中打开网站,自动提示进行安装,安装的过程很简单,安装结束后即可以使用. 安装包创建的数据库 ...
- JS里取前天,昨天和今天
var today=new Date(); var yesterday=new Date(today.getTime()-1000*60*60*24); var thedaybeforeyesterd ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-TwinCAT自带的找原点功能块MC_Home怎么用
对于相对编码器类型轴(包括虚拟轴),可以使用贝福提供的找原点功能块MC_Home. HomingMode是指机器在往前跑的时候(30单位/s的默认速度),当碰到阻挡,则会有一个布尔值从FALSE改 ...