300. Longest Increasing Subsequence

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.

首先是复杂度O(n^2)的动态规划算法。建立一个数组存放前i位的最长升序子序列,遍历0-j,如果nums[j]小于nums[i],用math.max(dp[j] + 1, dp[i])代替dp[i]。代码如下:

class Solution {
public:
    // There's a typical DP solution with O(N^2) Time and O(N) space 
    // DP[i] means the result ends at i
    // So for dp[i], dp[i] is max(dp[j]+1), for all j < i and nums[j] < nums[i]
    int lengthOfLIS(vector<int>& nums) {
        const int size = nums.size();
        if (size == 0) { return 0; } 
        vector<int> dp(size, 1);
        int res = 1;
        for (int i = 1; i < size; ++i) {
            for (int j = 0; j < i; ++j) {
                if (nums[j] < nums[i]) {
                    dp[i] = max(dp[i], dp[j]+1);
                }
            }
            res = max (res, dp[i]);
        }
        return res;
    }
};
第二种是复杂度O(nlogn)的二分查找方法。建立数组存放可能存在的升序子序列。从头遍历nums,试图把每一个num放到dp相应的位置里,最后返回dp存下的最长升序自序列的长度。代码如下:

public class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length];
        int size = 0;
        for (int x: nums) {
            int i = -1, j = size;
            while (i + 1 < j) {
                int mid = i + (j - i) / 2;
                if (dp[mid] < x) {
                    i = mid;
                } else {
                    j = mid;
                }
            }
            if (i < 0) {
                dp[0] = x;
            } else {
                dp[i + 1] = x;
            }
            if (i + 1 == size) {
                dp[size] = x;
                size ++;
            }
        }
        return size;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值