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 ...
随机推荐
- [转] Matlab与C++混合编程(依赖OpenCV)
作者 zouxy09@qq.com,原文 Matlab与C++混合编程(依赖OpenCV) 之前在运行别人论文的代码的时候,经常有遇到Matlab与C++混合编程的影子.实际上就是通过Matlab的M ...
- CentOS--在CentOS安装PHP5.6
查看centos的版本: [root@localhost ~]# lsb_release -a LSB Version: :core-3.1-amd64:core-3.1-ia32:core-3 ...
- Android Studio生成APK自动追加版本号、自定义apk名称、指定签名证书文件
你也可以查看我的其他同类文章,也会让你有一定的收货! 生成APK自动追加版本号 可自动区分debug和release,并追加版本号: 打开 build.gradle 在 android 节点中插入下面 ...
- windows SFC(System File Checker) 命令的使用
SFC(System File Checker)可以扫描所有受保护的系统文件的完整性,并使用正确的 Microsoft 版本替换. 步骤:点击开始,输入cmd: 右键,以管理员身份运行 输入sfc/s ...
- Oracle导入本属于sys用户的表
FlashBack Database后,将删除的数据导出时使用了system用户 exp system/oracle file=/home/oracle/test.dmp tables=sys.tes ...
- C#中的访问修饰符
1. 简述 private. protected. public. internal 修饰符的访问权限.private : 私有成员, 在类的内部才可以访问.protected : 保护成员,该类内部 ...
- postprocessing stack v2
用了v2和unity2017.3.0f有兼容性问题 在assetbundle的情况下 CopyStd这个shader打不进去 在assetbundle的menafest里面有列但是shader.fin ...
- mysql show profiles使用分析sql性能
mysql show profiles使用分析sql性能 Show profiles是5.0.37之后添加的,要想使用此功能,要确保版本在5.0.37之后. 查看一下我的数据库版本 mysql> ...
- Can we say objects have attributes, states and behaviors?
15down votefavorite 3 I was reading through Oracle's introduction to OOP concepts and I came across ...
- Android项目总结
功能: 1.图片载入 ImageLoader 參数配置要合理 cacheMemory 一次性的图片最好不要缓存在内存中 合理控制在内存中的内存大小 ,适当的释放 volley是googl ...