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)的更多相关文章

  1. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  2. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  3. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  4. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

  5. 300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. LeetCode Longest Increasing Subsequence (LIS O(nlogn))

    题意: 给一个数组,求严格递增的最长递增子序列的长度. 思路: 开销是一个额外的O(n)的数组.lower_bound(begin,end,val)的功能是:返回第一个大于等于val的地址. clas ...

  7. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  8. [Algorithms] Longest Increasing Subsequence

    The Longest Increasing Subsequence (LIS) problem requires us to find a subsequence t of a given sequ ...

  9. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

随机推荐

  1. Data Warehouse 简介

    数据仓库定义 数据仓库之父Bill Inmon在1991年出版的“Building the Data Warehouse”一书中所提出的定义被广泛接受:数据仓库(Data Warehouse)是一个面 ...

  2. Oracle Like、Instr以及正则表达式

    查看测试数据 select * from student; 1. like 在where字句中使用like可以达到模糊查询的效果,常用通配符如下 ▶ %: 使用 % 有三种情况 ① 字段 like ‘ ...

  3. React dangerouslySetInnerHTML - 将HTML字符串解析为html样式显示

    <div dangerouslySetInnerHTML={{ __html: curriculumDesc }}></div>

  4. centos7 多网卡修改默认路由

    最近在virtualbox里搭了一个centos7的虚拟机,但是网络这一块总是有问题. 单网卡下的问题: 1.当我配置连接方式为NAT网络地址转换的时候,虚拟机可以访问外网.但是在网络地址转换的情况下 ...

  5. 基于centos7实现的nfs

    NFS NFS(Network FileSystem,网络文件系统),最早由Sun公司所发展出来的,主要是通过网络让不同的主机.不同的操作系统,可以彼此分享个别档案,因此我们也可以简单把NFS看成是一 ...

  6. bootloader 关闭看门狗

    #define pWTCON 0x53000000disable_watchdog: ldr r0, =pWTCON mov r1, #0x0 str r1, [r0]

  7. 【c学习-2】

    #include <stdio.h> #define MYVAR 18 int main(){ //float x=3.14; //x=3.14++; //printf("%c\ ...

  8. tcl之string操作-length/index/range/replace

  9. 【转载】Callable、FutureTask中阻塞超时返回的坑点

    本文转载自:http://www.cnblogs.com/starcrm/p/5010863.html 案例1: package com.net.thread.future; import java. ...

  10. redhat 配置本地yum源

    redhat配置3个源就够了: 1.本地yum源,就是你本地的ISO 2.配置163源 3.配置epel源 环境:redhat7 + vmw 12 pro 1.配置本地yum源 要配置本地源,需要先把 ...