动态规划系列【2】最长递增子序列LIS

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?
Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

题意:无需多说。主要是这玩意有基础版O(n^2)和增强版O(nlogn)

public class Solution {
    public int lengthOfLIS(int[] nums) {
        int length=nums.length;
        if(length==0)return 0;
        int max=1;
        int[] d=new int[length];
        for(int i=1;i<length;i++){
            d[i]=1;
            for(int j=0;j<i;j++){
                if(nums[j]<nums[i])
                  d[i]=Math.min(d[i],d[j]=1);
            }
            max=Math.max(d[i],max);
        }
        return max;
      }
}

PS:思路参加http://www.hawstein.com/posts/dp-novice-to-advanced.html

public class AscentSequence {
    public int findLongest(int[] A, int n) {
        // write code here
        int[] dp=new int[n];
        int[] ends=new int[n];
        ends[0]=A[0];
        dp[0]=1;
        int max=1;
        int r=0,right=0,l=0,m=0;
        for(int i=1;i<n;i++){
            l=0;
            r=right;
            while(l<=r){
                m=(l+r)/2;
                if(A[i]>ends[m]){
                    l=m+1;
                }else{
                    r=m-1;
                }
            }
            //没明白
            right=Math.max(l,right);
            ends[l]=A[i];
            dp[i]=l+1;
            max=Math.max(dp[i],max);
        }
        return max;
    }
}

用二分查找、辅助数组。

时间: 2024-08-25 07:33:24

动态规划系列【2】最长递增子序列LIS的相关文章

动态规划(DP),最长递增子序列(LIS)

题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(dp[k])+1,(k<i),(a[k]<a[i]) #include <stdio.h> #define MAX 1005 int a[MAX];///存数据 int dp[MAX];///dp[i]表示以a[i]为结尾的最长递增子序列(LIS)的长度 int main() { int

算法--字符串:最长递增子序列LIS

转自:labuladong公众号 很多读者反应,就算看了前文 动态规划详解,了解了动态规划的套路,也不会写状态转移方程,没有思路,怎么办?本文就借助「最长递增子序列」来讲一种设计动态规划的通用技巧:数学归纳思想.  最长递增子序列(Longest Increasing Subsequence,简写 LIS)是比较经典的一个问题,比较容易想到的是动态规划解法,时间复杂度 O(N^2),我们借这个问题来由浅入深讲解如何写动态规划. 比较难想到的是利用二分查找,时间复杂度是 O(NlogN),我们通过

最长递增子序列 LIS 时间复杂度O(nlogn)的Java实现

关于最长递增子序列时间复杂度O(n^2)的实现方法在博客http://blog.csdn.net/iniegang/article/details/47379873(最长递增子序列 Java实现)中已经做了实现,但是这种方法时间复杂度太高,查阅相关资料后我发现有人提出的算法可以将时间复杂度降低为O(nlogn),这种算法的核心思想就是替换(二分法替换),以下为我对这中算法的理解: 假设随机生成的一个具有10个元素的数组arrayIn[1-10]如[2, 3, 3, 4, 7, 3, 1, 6,

算法面试题 之 最长递增子序列 LIS

找出最长递增序列 O(NlogN)(不一定连续!) 参考 http://www.felix021.com/blog/read.php?1587%E5%8F%AF%E6%98%AF%E8%BF%9E%E6%95%B0%E7%BB%84%E9%83%BD%E6%B2%A1%E7%BB%99%E5%87%BA%E6%9D%A5 我就是理解了一下他的分析 用更通俗易懂的话来说说题目是这样 d[1..9] = 2 1 5 3 6 4 8 9 7 要求找到最长的递增子序列首先用一个数组b[] 依次的将d里面

动态规划 - 最长递增子序列LIS

问题:一个序列有N个数:A[1],A[2],-,A[N],求出最长非降子序列的长度 样例输入:3 1 2 6 5 4 思路: 首先把问题简单化.可以先求A[1],...A[i]的最长非降子序列,令dp[i]为以A[i]结尾的最长非降子序列.当i = 1 时, 明显是长度dp[1] = 1 : i = 2 时,前面没有比1小的数字,故dp[2]=1 , 此时的最长非降子序列为1 ; i = 3 时,比数字2小的数是1 ,并且只有1 , 分析可知 dp[3] = dp[2]+1:当i = 4 时,找

POJ 1836 Alignment 最长递增子序列(LIS)的变形

大致题意:给出一队士兵的身高,一开始不是按身高排序的.要求最少的人出列,使原序列的士兵的身高先递增后递减. 求递增和递减不难想到递增子序列,要求最少的人出列,也就是原队列的人要最多. 1 2 3 4 5 4 3 2 1 这个序列从左至右看前半部分是递增,从右至左看前半部分也是递增.所以我们先把从左只右和从右至左的LIS分别求出来. 如果结果是这样的: A[i]={1.86 1.86 1.30621 2 1.4 1 1.97 2.2} //原队列 a[i]={1 1 1 2 2 1 3 4} b[

poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)

两种算法 1.  O(n^2) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 int a[1005]; 7 int dp[1005]; 8 int main() 9 { 10 int n, maxn; 11 while(scanf("%d", &n) != EOF) 12 { 13 maxn = 0; 14 for(

最长递增子序列 (LIS) Longest Increasing Subsequence

问题描述: 有一个长为n的数列a0, a1,..., an-1.请求出这个序列中最长的上升子序列.请求出这个序列中最长的上升子序列. 上升子序列:对于任意i<j都满足ai<aj的子序列. 限制条件 i <= n <= 1000 0 <= ai <= 1000000 两种定义方式 具体看程序注释 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #inc

最长递增子序列LIS递归算法

#include<iostream> using namespace std; int minStep,n,*arr,*record,*lis,index,recordMax,lisCount; /* 1.minStep :存放"只"遍历一次指定数组,得到的LIS的长度.比如: *arr={4,5,1,2,3}; 遍历该数组过后,minStep=2,即为{4,5} 两个元素的长度.具体请看getMinStep方法. 2.*arr : 存放输入的,或者随机产生的一组数; 3.