LintCode -- 最长上升子序列 O(nlgn)

LintCode -- longest-increasing-subsequence(最长上升子序列)


原题链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-subsequence/


给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度。


样例

给出[5,4,1,2,3],这个LIS是[1,2,3],返回 3

给出[4,2,4,5,3,7],这个LIS是[4,4,5,7],返回 4

挑战

要求时间复杂度为O(n^2) 或者O(nlogn)

说明

最长上升子序列的定义:

  • 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的。
  • https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

分析:

dp [ i ] 表示长度为 i 的上升子序列的最后一个元素的最小值。容易证明,dp 是一个递增数组。

扫描给定 nums 数组,在 dp 数组中查找比 nums[ j ] 小的数中下标最大值 x ,更新 dp,dp[ x+1 ] = min( dp[ x+1 ], nums[ i ] )。

****  时间复杂度  O(nlgn), 空间复杂度 O(n)****


代码【O( nlgn )】(C++、Java):

class Solution {
public:
    /**
     * @param nums: The integer array
     * @return: The length of LIS (longest increasing subsequence)
     */
    int min(int a, int b){    
        if (a < b) return a;
        else return b;
    }
    int search(vector<int> dp, int i, int j, int k){
        if (i == j) return i;
        else{
            int p = (i+j) / 2;
            if(dp[p] > k) return search(dp, i, p-1, k);
            else if ((dp[p] < k && dp[p+1] <= k) || (dp[p] == k && dp[p+1] == k))
                return search(dp, p+1, j, k);
            else return p;
        }
    }
    int longestIncreasingSubsequence(vector<int> nums) {
        // write your code here        O(nlgn)
        int n = nums.size();
        if (n <= 1) return n;
        vector<int> dp;
        int res = 1, x;
        dp.push_back(0);
        dp.push_back(1e9);
        for (int i = 0; i < n; i++){
            dp.push_back(1e9);
            x = search(dp, 1, res, nums[i]);
            if (x == 1 && nums[i] < dp[x]) dp[x] = nums[i];
            else{
                if (x == res) res += 1;
                dp[x+1] = min(dp[x+1], nums[i]);
            }
        }
        return res;
    }
};
public class Solution {
    /**
     * @param nums: The integer array
     * @return: The length of LIS (longest increasing subsequence)
     */
    int min(int a, int b){    
        if (a < b) return a;
        else return b;
    }
    int search(int [] dp, int i, int j, int k){
        if (i == j) return i;
        else{
            int p = (i+j) / 2;
            if(dp[p] > k) return search(dp, i, p-1, k);
            else if ((dp[p] < k && dp[p+1] <= k) || (dp[p] == k && dp[p+1] == k))
                return search(dp, p+1, j, k);
            else return p;
        }
    }
    public int longestIncreasingSubsequence(int[] nums) {
        // write your code here       O(nlgn)
        int n = nums.length;
        if (n <= 1) return n;
        int [] dp = new int [n+1+10];
        int res = 1, x;
        dp[0] = 0;
        dp[1] = 1000000;
        for (int i = 0; i < n; i++){
            dp[i+2] = 1000000;
            x = search(dp, 1, res, nums[i]);
            if (x == 1 && nums[i] < dp[x]) dp[x] = nums[i];
            else{
                if (x == res) res += 1;
                dp[x+1] = min(dp[x+1], nums[i]);
            }
        }
        return res;
    }
}


代码【O( n^2 )】(C++、Python):

class Solution {
public:
    /**
     * @param nums: The integer array
     * @return: The length of LIS (longest increasing subsequence)
     */
    int max(int a, int b){
        if (a > b) return a;
        else return b;
    }
    int longestIncreasingSubsequence(vector<int> nums) {
        // write your code here   O(n^2)
        int n = nums.size();
        if (n <= 1) return n;
        vector<int> dp;
        int res = 1;
        dp.push_back(1);
        for (int i = 1; i < n; i++){
            dp.push_back(1);
            for (int j = 0; j < i; j++)
                if (nums[j] <= nums[i])
                    dp[i] = max(dp[i], dp[j] + 1);
                if (dp[i] > res) res = dp[i];
        }
        return res;
    }
};

public class Solution {
    /**
     * @param nums: The integer array
     * @return: The length of LIS (longest increasing subsequence)
     */
    int max(int a, int b){
        if (a > b) return a;
        else return b;
    }
    public int longestIncreasingSubsequence(int[] nums) {
        // write your code here    O(n^2)
        int n = nums.length;
        if (n <= 1) return n;
        int [] dp = new int [n+1]; 
        int res = 1;
        dp[0] = 1;
        for (int i = 1; i < n; i++){
            dp[i] = 1;
            for (int j = 0; j < i; j++)
                if (nums[j] <= nums[i])
                    dp[i] = max(dp[i], dp[j] + 1);
                if (dp[i] > res) res = dp[i];
        }
        return res;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值