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 ...
随机推荐
- windows下配置kafka
https://blog.csdn.net/evankaka/article/details/52421314
- Spring的jdbcTemplate 与原始jdbc 整合c3p0的DBUtils 及Hibernate 对比 Spring配置文件生成约束的菜单方法
以User为操作对象 package com.swift.jdbc; public class User { private Long user_id; private String user_cod ...
- Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'name pattern' at character position 36
例: <aop:config> <aop:pointcut expression="execution(* com.zsn.Service.Impl.*.*(..))&qu ...
- mysql数据库设置外键,更新与删除选项
CASCADE:父表delete.update的时候,子表会delete.update掉关联记录:SET NULL:父表delete.update的时候,子表会将关联记录的外键字段所在列设为null, ...
- Maven - 依赖范围<scope></scope>
6种:
- django+xadmin在线教育平台(七)
4-3 新建项目 Python2.7 创建虚拟环境. mkvirtualenv mxonline2 安装django pip install django==1.9.8 注意Python2下此处必须用 ...
- js | javascript实现浏览器窗口大小被改变时触发事件的方法
转载 当浏览器的窗口大小被改变时触发的事件window.onresize 为事件指定代码: 代码如下: window.onresize = function(){ } 例如: 浏览器可见区域信息: 代 ...
- JAVA / MySql 编程——第五章 事务、视图、索引、备份和恢复
1.事务(Transaction): 事务是将一系列数据操作绑成一个整体进行统一管理. 如果一事务执行成功,则咋子该事务中进行的所有数据更改均会提交,称为数据库中的永久成部分. 如果事务执行是遇到错误 ...
- Docker虚拟化容器的使用
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Li ...
- python--Matplotlib(二)
Matplotlib+pandas作图 一.对csv文件进行提取ruixi.csv 对上述表格进行提取并做图 #-*- coding:utf-8 -*- import matplotlib as mp ...