300. Longest Increasing Subsequence

本文探讨了在未排序整数数组中寻找最长递增子序列长度的问题,提供了两种算法解决方案:动态规划和结合二分查找的动态规划,后者时间复杂度更优,为O(nlogn)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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?

Approach #1 Dynamic Programing
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int len = nums.size();
        if (len == 0) return 0;
        int ans = 1;
        vector<int> temp(len, 1);
        for (int i = len-2; i >= 0; --i) {
            for (int j = i; j < len; ++j) {
                if (nums[i] < nums[j]) {
                    temp[i] = max(temp[i], temp[j] + 1);
                }
            }           
            ans = max(ans, temp[i]);
        }
        return ans;
    }
};

Runtime: 24 ms, faster than 26.23% of C++ online submissions for Longest Increasing Subsequence.

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        vector<int> temp;
        for (int i = 0; i < nums.size(); ++i) {
            auto it = std::lower_bound(temp.begin(), temp.end(), nums[i]);
            if (it == temp.end()) temp.push_back(nums[i]);
            else *it = nums[i];
        }
        return temp.size();
    }
};

Runtime: 4 ms, faster than 72.86% of C++ online submissions for Longest Increasing

Analysis:
lower_bound(temp.begin(), temp.end(), nums[i]) return the first element's address which more or equal to nums[i].
In this approach we use a vector to store the LIS nums.

Our strategy determined by the following conditions,

  1. If A[i] is smallest among all end
    candidates of active lists, we will start
    new active list of length 1.
  2. If A[i] is largest among all end candidates of
    active lists, we will clone the largest active
    list, and extend it by A[i].
  1. If A[i] is in between, we will find a list with
    largest end element that is smaller than A[i].
    Clone and extend this list by A[i]. We will discard all
    other lists of same length as that of this modified list.

here is an example:
It will be clear with an example, let us take example from wiki {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}.

A[0] = 0. Case 1. There are no active lists, create one.


A[1] = 8. Case 2. Clone and extend.

  1. 0, 8.

A[2] = 4. Case 3. Clone, extend and discard.

  1. 0, 4.
    0, 8.Discarded

A[3] = 12. Case 2. Clone and extend.

  1. 0, 4.
    0, 4, 12.

A[4] = 2. Case 3. Clone, extend and discard.

  1. 0, 2.
    0, 4.Discarded.
    0, 4, 12.

A[5] = 10. Case 3. Clone, extend and discard.

  1. 0, 2.
    0, 2, 10.
    0, 4, 12. Discarded.

A[6] = 6. Case 3. Clone, extend and discard.

  1. 0, 2.
    0, 2, 6.
    0, 2, 10. Discarded.

A[7] = 14. Case 2. Clone and extend.

  1. 0, 2.
    0, 2, 6.
    0, 2, 6, 14.

A[8] = 1. Case 3. Clone, extend and discard.

  1. 0, 1.
    0, 2. Discarded.
    0, 2, 6.
    0, 2, 6, 14.

A[9] = 9. Case 3. Clone, extend and discard.

  1. 0, 1.
    0, 2, 6.
    0, 2, 6, 9.
    0, 2, 6, 14.Discarded.

A[10] = 5. Case 3. Clone, extend and discard.

  1. 0, 1.
    0, 1, 5.
    0, 2, 6.Discarded.
    0, 2, 6, 9.

A[11] = 13. Case 2. Clone and extend.

  1. 0, 1.
    0, 1, 5.
    0, 2, 6, 9.
    0, 2, 6, 9, 13.

A[12] = 3. Case 3. Clone, extend and discard.

  1. 0, 1.
    0, 1, 3.
    0, 1, 5. Discarded.
    0, 2, 6, 9.
    0, 2, 6, 9, 13.

A[13] = 11. Case 3. Clone, extend and discard.

  1. 0, 1.
    0, 1, 3.
    0, 2, 6, 9.
    0, 2, 6, 9, 11.
    0, 2, 6, 9, 13. Discarded.

A[14] = 7. Case 3. Clone, extend and discard.

  1. 0, 1.
    0, 1, 3.
    0, 1, 3, 7.
    0, 2, 6, 9. Discarded.
    0, 2, 6, 9, 11.

A[15] = 15. Case 2. Clone and extend.

  1. 0, 1.
    0, 1, 3.
    0, 1, 3, 7.
    0, 2, 6, 9, 11.
    0, 2, 6, 9, 11, 15. <-- LIS List

转载于:https://www.cnblogs.com/ruruozhenhao/p/9898933.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值