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 ...
随机推荐
- ORMLite整合SQLCipher
Android数据库加密,目前就是SQLCipher对SQLite整体加密,微信也是使用这种方式.开源,且支持很多平台. SQLCipher虽说开源了,但是编译好的jar和so文件,还是要收费的. 但 ...
- 一道综合练习题实践list及dictionary集合类
定义一个员工的集合,对员工集合内的元素进行查询和删除.实现员工的签到和签退,要求如下: //A:每天只能签到一次 //B:签退前必须已经签到 //C:显示打卡记录 代码如下:员工信息类: using ...
- Inno Setup入门(十四)——替换安装程序和卸载程序的图标
通常Inno生成的安装文件的图标是一个光盘和显示器,如下图.同时,程序安装好之后,在安装目录下的卸载程序的图标也是一样的,其实我们也可以自己修改. 首先生成的安装文件图标.这个比较简单,只需要在Set ...
- Java汉字md5值不一致问题
原文:http://blog.csdn.net/earthhour/article/details/51188437 通过main方法测试得到一个加密值,通过servlet request调用得到一个 ...
- inline-block空隙总结
如果inline-block,宽度都是50%会留有空隙,解决方法如下 1.标签之间不留空格 (1)直接不留空 <div></div><div></div> ...
- Java几种常见的四舍五入的方法
/* * 在上面简单地介绍了银行家舍入法,目前java支持7中舍入法: 1. ROUND_UP:远离零方向舍入.向绝对值最大的方向舍入,只要舍弃位非0即进位. 2. ROUND_DOWN:趋向零方向舍 ...
- vagrant 知识库
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://wushaobo.info/?p=83 Vagrant让虚拟化技术走近寻常家.脚踏实地地说,网络上类似“两分钟入门”的文 ...
- NFSv4 mount incorrectly shows all files with ownership as nobody:nobody
NFSv4 mount incorrectly shows all files with ownership as nobody:nobody https://access.redhat.com/ ...
- POJ 3077-Rounders(水题乱搞)
Rounders Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7697 Accepted: 4984 Descript ...
- 两列布局(浮动、定位、flex)和三列布局(圣杯、双飞翼、flex)
demo 各种布局演示 https://jsfiddle.net/mayufo/qp890peq/1/ 两栏布局 浮动 <div class="box1"> <d ...