300. Longest Increasing Subsequence

Posted ruruozhenhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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.

0.

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

  1. 0, 8.

    A[2] = 4. Case 3. Clone, extend and discard.
  2. 0, 4.
    ~~0, 8. ~~Discarded
    -----------------------------------------------------------------------------
    A[3] = 12. Case 2. Clone and extend.
  3. 0, 4.
    0, 4, 12.
    -----------------------------------------------------------------------------
    A[4] = 2. Case 3. Clone, extend and discard.
  4. 0, 2.
    ~~0, 4. ~~Discarded.
    0, 4, 12.
    -----------------------------------------------------------------------------
    A[5] = 10. Case 3. Clone, extend and discard.
  5. 0, 2.
    0, 2, 10.
    0, 4, 12. Discarded.
    -----------------------------------------------------------------------------
    A[6] = 6. Case 3. Clone, extend and discard.
  6. 0, 2.
    0, 2, 6.
    0, 2, 10. Discarded.
    -----------------------------------------------------------------------------
    A[7] = 14. Case 2. Clone and extend.
  7. 0, 2.
    0, 2, 6.
    0, 2, 6, 14.
    -----------------------------------------------------------------------------
    A[8] = 1. Case 3. Clone, extend and discard.
  8. 0, 1.
    0, 2. Discarded.
    0, 2, 6.
    0, 2, 6, 14.
    -----------------------------------------------------------------------------
    A[9] = 9. Case 3. Clone, extend and discard.
  9. 0, 1.
    0, 2, 6.
    0, 2, 6, 9.
    ~~0, 2, 6, 14. ~~Discarded.
    -----------------------------------------------------------------------------
    A[10] = 5. Case 3. Clone, extend and discard.
  10. 0, 1.
    0, 1, 5.
    ~~0, 2, 6. ~~Discarded.
    0, 2, 6, 9.
    -----------------------------------------------------------------------------
    A[11] = 13. Case 2. Clone and extend.
  11. 0, 1.
    0, 1, 5.
    0, 2, 6, 9.
    0, 2, 6, 9, 13.
    -----------------------------------------------------------------------------
    A[12] = 3. Case 3. Clone, extend and discard.
  12. 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.
  13. 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.
  14. 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.
  15. 0, 1.
    0, 1, 3.
    0, 1, 3, 7.
    0, 2, 6, 9, 11.
    0, 2, 6, 9, 11, 15. <-- LIS List
    ----------------------------------------------------------------------------

以上是关于300. Longest Increasing Subsequence的主要内容,如果未能解决你的问题,请参考以下文章

300. Longest Increasing Subsequence

300. Longest Increasing Subsequence

300. Longest Increasing Subsequence

300. Longest Increasing Subsequence

300. Longest Increasing Subsequence

300. Longest Increasing Subsequence