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.
0.
0, 8.


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


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


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


A[5] = 10. Case 3. Clone, extend and discard.
0.
0, 2.
0, 2, 10.
0, 4, 12. Discarded.


A[6] = 6. Case 3. Clone, extend and discard.
0.
0, 2.
0, 2, 6.
0, 2, 10. Discarded.


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


A[8] = 1. Case 3. Clone, extend and discard.
0.
0, 1.
0, 2. Discarded.
0, 2, 6.
0, 2, 6, 14.


A[9] = 9. Case 3. Clone, extend and discard.
0.
0, 1.
0, 2, 6.
0, 2, 6, 9.
0, 2, 6, 14.Discarded.


A[10] = 5. Case 3. Clone, extend and discard.
0.
0, 1.
0, 1, 5.
0, 2, 6.Discarded.
0, 2, 6, 9.


A[11] = 13. Case 2. Clone and extend.
0.
0, 1.
0, 1, 5.
0, 2, 6, 9.
0, 2, 6, 9, 13.


A[12] = 3. Case 3. Clone, extend and discard.
0.
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.
0.
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.
0.
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.
0.
0, 1.
0, 1, 3.
0, 1, 3, 7.
0, 2, 6, 9, 11.
0, 2, 6, 9, 11, 15. <-- LIS List


2021-04-27 23:34:48 星期二
给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。

子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。

示例 1:

输入:nums = [10,9,2,5,3,7,101,18]
输出:4
解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。
示例 2:

输入:nums = [0,1,0,3,2,3]
输出:4
示例 3:

输入:nums = [7,7,7,7,7,7,7]
输出:1

提示:

1 <= nums.length <= 2500
-104 <= nums[i] <= 104

进阶:

你可以设计时间复杂度为 O(n2) 的解决方案吗?
你能将算法的时间复杂度降低到 O(n log(n)) 吗?

思路:DP问题,维护一个一维的dp数组用来记录当前位置的最长递增子序列的长度。每计算一个位置的最长递增子序列时,都要与之前的数字进行比较
if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] + 1)
image

Code:

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int len = nums.size();
        vector<int> dp(len, 1);
        for (int i = 0; i < len; ++i) {
            for (int j = 0; j < i; ++j) {
                if (nums[i] > nums[j]) {
                    dp[i] = max(dp[i], dp[j] + 1);
                }
            }
        }
        return *max_element(dp.begin(), dp.end());
    }
};
posted @ 2018-11-02 22:37  Veritas_des_Liberty  阅读(180)  评论(0编辑  收藏  举报