最长上升子序列Longest Increasing Subsequence

给定一个无序的整数数组,找到其中最长上升子序列的长度。

示例:

输入: [10,9,2,5,3,7,101,18]
输出: 4
解释: 最长的上升子序列是?[2,3,7,101],它的长度是 4。

方法1:时间复杂度n2,容易想到,记录数组中每个元素作为上升子序列最后一个元素的最大长度。

import java.math.*;
class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] res = new int[nums.length];
        int ret=0;
        for(int i =0;i<nums.length;i++){
            res[i] = 1;
        }
        for(int i = 0;i<nums.length;i++){
            for(int j = 0;j<i;j++){
                if(nums[j]<nums[i])
                    res[i] = Math.max(res[i],res[j]+1);
            }
            ret = Math.max(ret,res[i]);
        }
         return ret;
    }

}

方法2: 此处用到了二分查找,dp[i]数组保存的是长度为i的上升子序列最小尾数。所以dp数组为有序数组,每次遍历一个新的数x时,只需要查找dp数组中大于等于x的最小元素,并替换掉。

import java.math.*;
class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length];
        if(nums.length==0)  return 0;
        int res = 0;
        for(int i =0;i<nums.length;i++){
            dp[i] = 0;
        }
        int low = 0,high =0,mid;
        dp[0] = nums[0];
        res = 1;
        for(int i =1;i<nums.length;i++){
      //     System.out.println(low+" "+high);
            while(low <= high){
                mid = (low+high)/2;
                if(dp[mid]>=nums[i]){
                    high = mid - 1;
                }else{
                    low = mid + 1;
                }
            }
            dp[low] = nums[i];
          // System.out.println(low+"-");
            res = Math.max(res,low+1);
            high = res-1;
            low = 0;
      //      for(int j =0;j<nums.length;j++){
     //   System.out.print(dp[j]+" ");
      //  }
        //  System.out.println("");
        }

        return res;
    }
}

原文地址:https://www.cnblogs.com/a1225234/p/11269261.html

时间: 2024-10-03 17:06:11

最长上升子序列Longest Increasing Subsequence的相关文章

nlog(n)解动态规划--最长上升子序列(Longest increasing subsequence)

最长上升子序列LIS问题属于动态规划的初级问题,用纯动态规划的方法来求解的时间复杂度是O(n^2).但是如果加上二叉搜索的方法,那么时间复杂度可以降到nlog(n).  具体分析参考:http://blog.chinaunix.net/uid-26548237-id-3757779.html 代码: #include <iostream> using namespace std; int LIS_nlogn(int *arr, int len) { int *LIS = new int[len

最长递增子序列(Longest increasing subsequence)

问题定义: 给定一个长度为N的数组A,找出一个最长的单调递增子序列(不要求连续). 这道题共3种解法. 1. 动态规划 动态规划的核心是状态的定义和状态转移方程.定义lis(i),表示前i个数中以A[i]结尾的最长递增子序列的长度.可以得到以下的状态转移方程: d(i) = max(1, d(j) + 1), 其中j < i,且A[j] <= A[i] 程序实现: int longestIncreasingSubsequence(vector<int> nums) { if (nu

算法实践--最长递增子序列(Longest Increasing Subsquence)

什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5}, {1} 其中最长的递增子序列是{2, 4, 5} 问题:对于长度为N的矢量D,如何找到它的最长递增子序列 一个简单的算法 for (i=N; i>0; --i) {1. 找到所有长度为i的子序列; //复杂度为(N!)/(i!)(N-i)! O(exp(N)) 2. 判断是否其中有一个为递增子

LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be

LeetCode 673. Number of Longest Increasing Subsequence 最长递增子序列的个数 (C++/Java)

题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] O

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

Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be more

SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929 Description Given a sequence of N pairs of integers, find the length of the longest incre

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Outpu

Dynamic Programming | Set 3 (Longest Increasing Subsequence)

在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Optimal Substructure Property) 中我们已经讨论了重叠子问题和最优子结构性质,现在我们来看一个可以使用动态规划来解决的问题:最长上升子序列(Longest Increasing Subsequence(LIS)). 最长上升子序列问题,致力于在一个给定的序列中找到一个最长的子序列