LeetCode -- Longest Increasing Subsequence(LIS)
Question:
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.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
Analysis:
给出一个由正整数构成的数组,找出里面最长的子序列。(序列,不要求每个数字都是连续的)
例如:给出数组{10, 9, 2, 5, 3, 7, 101, 18},最长的子序列为{2, 3, 7, 101},因此长度为4. 注意可能有不止一个LIS序列,这里仅仅要求你返回他们的长度。
要求算法的时间复杂度为O(n2).
Follow up: 你可以将时间复杂度提升到O(nlogn)嘛?
思路:由于前面做过判断是否存在长度为3的递增子序列,按照相似的思路,乍一眼看到这个问题感觉比较简单,可以很容易的解决,结果后面越分析越复杂。只管来说应该按照DP的思想解决,但是前面做过N多关于DP的题目了,仍然对这类题目还是不开窍。。好郁闷。。因此在网上参考了九章算术的答案。具体思路是,用一个额外的数组现将到该位置时的子序列长度设为1,然后从第一个元素开始往当前元素循环(简单来说就是加一层循环看前面有几个元素比当前元素小),然后更新result最为最终的返回结果。
(通过上面及以前的分析可知,一般动态规划的题目都可以牺牲一点空间复杂度来达到目的,如果暂时想不出DP的状态转化公式且题目没有明显的要求空间复杂度时可考虑先用额外的数组等来存储每步的转态,至少保证能够解答出题目)。
Answer:
public class Solution {
public int lengthOfLIS(int[] nums) {
if(nums == null)
return 0;
int[] num = new int[nums.length];
int result = 0;
for(int i=0; i<nums.length; i++) {
num[i] = 1;
for(int j=0; j<i; j++) {
if(nums[j] < nums[i]) {
num[i] = num[i] > num[j] + 1 ? num[i] : num[j] +1;
}
}
if(num[i] > result)
result = num[i];
}
return result;
}
}
LeetCode -- Longest Increasing Subsequence(LIS)的更多相关文章
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LeetCode] 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 ...
- 300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- LeetCode Longest Increasing Subsequence (LIS O(nlogn))
题意: 给一个数组,求严格递增的最长递增子序列的长度. 思路: 开销是一个额外的O(n)的数组.lower_bound(begin,end,val)的功能是:返回第一个大于等于val的地址. clas ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- [Algorithms] Longest Increasing Subsequence
The Longest Increasing Subsequence (LIS) problem requires us to find a subsequence t of a given sequ ...
- 【Lintcode】076.Longest Increasing Subsequence
题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...
随机推荐
- 【Java】数组知识回顾
package another; import java.util.Arrays; import java.util.List; /** * 数组知识回顾 * @author ChristineBas ...
- 10-UIScrollView
UIScrollView 掌握 UIScrollView的常见属性 UIScrollView的常用代理方法 UIScrollView的缩放 UIScrollView和UIPageControl的分页 ...
- 表单验证实现React-router跳转
方法一:broserHistory.push handleSubmit(e){ e.preventDefault(); const path = '/demo'; broserHistory.push ...
- HJ浇花
题目描述 HJ养了很多花(99999999999999999999999999999999999盆),并且喜欢把它们排成一排,编号0~999999999999999999999999999999999 ...
- Linux自带mariadb卸载
MySQL安装过程中报错: dpkg: regarding mysql-community-server_5.6.39-1debian9_i386.deb containing mysql-commu ...
- Redis ---------- Sort Set排序集合类型
sortset是(list)和(set)的集中体现 与set的相同点: string类型元素的集合 不同点: sortset的元素:值+权 适合场合 获得最热门前5个帖子的信息 例如 select * ...
- SQL Server 数据库内部版本号
数据库还原或版本升级出现版本错误时可参考 可以利用SQL语句查看本机数据库版本:select @@VERSION 如果你不确定是源数据库的版本,下面的映射表的SQL Server版本的内部版本号,以便 ...
- C++基础 inline 默认参数 函数占位参数 函数重载
1. inline内联函数 内联函数用于替换宏, 实例: 其中宏和 ++ 连用有副作用. #include "iostream" using namespace std; #def ...
- scala高级性质-隐式转换 -02
今天我们来介绍scala的高级特性,上次已经介绍过他的一个特性:高阶函数,这次是隐式转换 1.隐式转换的例子 read的例子 解析:发现这个file没有read的方法,然后就开始在开始在这个上下文里面 ...
- PHP代码审计5-实战漏洞挖掘-cms后台登录绕过
cms后台登录绕过 练习源码:[来源:源码下载](数据库配置信息有误,interesting) 注:需进行安装 1.创建数据库 2.设置账号密码,连接数据库 3.1 正常登录后台,抓包分析数据提交位置 ...