问题描述:给出一个序列a1,a2,a3,a4,a5,a6,a7….an,求它的一个子序列(设为s1,s2,…sn),使得这个子序列满足这样的性质,s1<s2<s3<…<sn并且这个子序列的长度最长。简称LIS。

例如,对于数组[10, 9, 2, 5, 3, 7, 101, 18],它的最长递增子序列为[2,3,7,101],长度为4。当然最长递增子序列不止一个,例如[2,5,7,101],[2,5,7,18]都符合要求。

1.时间复杂度:O(n^2)

对于序列A(a1,a2,a3…..an),以序列中am元素为最小值的递增序列,只与am-1,am-2,……an有关,与am之前的元素无关,因此我们可以由后向前依次求出以每个元素为起始的最长递增序列。我们用一个数组flag存储最长递增子序列的长度,若i<j,A[i] < A[j],则flag[i] = max(flag[x], j <= x <= A.length) + 1。

   1: public int lengthOfLIS(int[] nums) {

   2:                 if (nums == null || nums.length == 0)

   3:                         return 0;

   4:                 int[] flag = new int[nums.length];

   5:                 Arrays.fill(flag,1);

   6:                 int maxLength = 1;

   7:                 for (int i =nums.length - 2; i >= 0; i--){

   8:                         int maxTemp = flag[i];

   9:                         for (int j = i+1; j < nums.length;j++){

  10:                                 if (nums[i] < nums[j]){

  11:                                         maxTemp = maxTemp > flag[j] + 1 ? maxTemp : flag[j] + 1;

  12:                                 }

  13:                         }

  14:                         flag[i] = maxTemp;

  15:                         maxLength = maxLength > flag[i] ? maxLength: flag[i];

  16:                 }

  17:                 return maxLength;

  18:         }

2.时间复杂度O(nlogN)

我们定义一个数组dp[],dp[i]表示长度为i的序列中最小的元素的值。例如对于序列A[10, 9, 2, 5, 3, 7, 101, 18],我们依次扫描每个元素,

首先对于A[0] = 10 , 我们可以得dp[1] = 10 ,表示序列长度为1的最小元素为10,此时序列为10,

对于A[1] = 9, 因为A[1] < dp[1],所以dp[1] = 9,此时序列为9

对于A[2] = 2, 因为A[2] < dp[1],所以dp[1] = 2,此时序列为2

对于A[3] = 5, 因为A[3] > dp[1],所以dp[2] = 5,此时序列为2,5

对于A[4] = 3, 因为A[4] < dp[2],所以dp[2] = 3,此时序列为2,3

对于A[5] = 7, 因为A[5] > dp[2],所以dp[3] = 7,此时序列为2,3,7

对于A[6] = 101, 因为A[6] > dp[3],所以dp[3] = 101,此时序列为2,3,7,101

对于A[7] = 18, 因为A[7] < dp[3],所以dp[3] = 18,此时序列为2,5,7,18

所以最长递增子序列的长度为4

   1: int[] dp = new int[nums.length];

   2:                 int len = 0;

   3:  

   4:                 for(int x : nums) {

   5:                         int i = Arrays.binarySearch(dp, 0, len, x);

   6:                         if(i < 0) i = -(i + 1);

   7:                         dp[i] = x;

   8:                         if(i == len) len++;

   9:                 }

  10:  

  11:                 return len;

Longest Increasing Subsequence的两种算法的更多相关文章

  1. Longest Increasing Subsequence的两种解法

    问题描述: 给出一个未排序队列nums,如[10, 9, 2, 5, 3, 7, 101, 18].找出其中最长的增长序列,但不是连续增长序列,如[2, 3, 7, 101]就是对应的最长增长序列LI ...

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

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

  3. 最长上升子序列 LIS(Longest Increasing Subsequence)

    引出: 问题描述:给出一个序列a1,a2,a3,a4,a5,a6,a7….an,求它的一个子序列(设为s1,s2,…sn),使得这个子序列满足这样的性质,s1<s2<s3<…< ...

  4. Longest Increasing Subsequence - LeetCode

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

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

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  6. [tem]Longest Increasing Subsequence(LIS)

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

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

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

  8. 300最长上升子序列 · Longest Increasing Subsequence

    [抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...

  9. 673. Number of Longest Increasing Subsequence最长递增子序列的数量

    [抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...

随机推荐

  1. live555 基本类之间的关系

    live555 中存在这5个最基本的类.每个类中都拥有一个BasicUsageEnvironment. 这是这几个类之间的相互关系.   MediaSession可以拥有多个subsession.

  2. 使用 StoryBoard 制作一个能够删除cell的TableView

    本篇博客方便自己检索使用.资源链接 下面是制作效果图,点击删除按钮,就能将该cell删除: 下面是主要的代码: #define KSUPER_TAG 20000 #define KDEFAU_TAG ...

  3. Maven安装,配置及更改本地资源库

    安装 1.确保已安装JDK,和配置JAVA_HOME环境变量 . 注:Maven 3.2 要求 JDK 1.6 或以上版本, 而 Maven 3.0/3.1 需要 JDK 1.5 或以上,这里安装的是 ...

  4. c/c++面试39-44之内存动态分配

    39 指针的初始化(二叉树排序),其中引入了双向链表 #include <stdio.h> #include <stdlib.h> struct Tag_Node { stru ...

  5. TypeScript完全解读(26课时)_19.其他重要更新

    ts3.3升级过来有很多重要的更新 没法归类的更新,在本节课几种讲一下 创建update.ts,然后在index.ts内引入 async和promise es6中增加了promise的支持,能够很好处 ...

  6. 为什么用思科里面的设备第一次ping的时候总会丢一个包呢?

    大家搞计算机的不用讲,肯定都玩过网络吧?   比如一些思科,华为,华三这些模拟器,你们总会当你第一次用某个设备去ping某个设备的时候第一包总会被丢弃.  但我相信很多人都不知道为啥 会丢弃. 今天小 ...

  7. Node.js 使用Stream的pipe(管道)方法实现文件复制

    Stream模块有一个pipe方法,可以将两个流串起来,实现所有的数据自动从Readable流进入Writable流 "use strict"; const fs = requir ...

  8. 大数据技术之_27_电商平台数据分析项目_02_预备知识 + Scala + Spark Core + Spark SQL + Spark Streaming + Java 对象池

    第0章 预备知识0.1 Scala0.1.1 Scala 操作符0.1.2 拉链操作0.2 Spark Core0.2.1 Spark RDD 持久化0.2.2 Spark 共享变量0.3 Spark ...

  9. 用EnumMap代替序数索引

    用EnumMap代替序数索引   有时候,会见到利用ordinal方法来索引数组的代码.例如下面这个简化的类,表示一种烹饪用的香草: public class Herb { public enum T ...

  10. PJzhang:robots协议的实际场景

    猫宁!!! 参考链接: https://bbs.360.cn/thread-15062960-1-1.html https://ziyuan.baidu.com/college/courseinfo? ...