LeetCode刷题 Day52

54 阅读1分钟

LeetCode刷题 Day52

300. Longest Increasing Subsequence

Given an integer array nums, return the length of the longest strictly increasing subsequence

Example 1:

Input: nums = [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.

Example 2:

Input: nums = [0,1,0,3,2,3]
Output: 4

Example 3:

Input: nums = [7,7,7,7,7,7,7]
Output: 1

步骤:

  1. dp index和value: index同nums的index, value就是上升序列长度
  2. 递推公式: dp[i] = Math.max(dp[i], dp[j] + 1);
  3. 初始化: 因为每一个独立元素都是一个长度所以都初始化为1
  4. 遍历顺序: 双层顺序遍历,
for (i = 1; i < length; i++) {
    for (let j = 0; j < i; j++) {
    }
}

代码:

var lengthOfLIS = function(nums) {
    let length = nums.length; 
    if (length <= 1) return length;

    let dp = Array(length).fill(1);
    let res = 0;

    for (let i = 1; i < length; i++) {
        for (let j = 0; j < i; j++) {
            if (nums[i] > nums[j]) dp[i] =  Math.max(dp[i], dp[j] + 1);
        }
        res = Math.max(dp[i], res);
    }
    
    return res;
}

时间复杂度: O(n2) 空间复杂度: O(n)


674. Longest Continuous Increasing Subsequence

Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray) . The subsequence must be strictly increasing.

continuous increasing subsequence is defined by two indices l and r (l < r) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] and for each l <= i < rnums[i] < nums[i + 1].

Example 1:

Input: nums = [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3.
Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element
4.

Example 2:

Input: nums = [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly
increasing.

代码:

var findLengthOfLCIS = function(nums) {
    let maxLen = 1;
    let currLen = 1;
    for (let i = 1; i < nums.length; i++) {
        if (nums[i - 1] < nums[i]) {
            currLen++;
        } else {
            maxLen = Math.max(maxLen, currLen);
            currLen = 1;
        }
    }

    return Math.max(maxLen, currLen);
};

时间复杂度: O(n) 空间复杂度: O(1)


718. Maximum Length of Repeated Subarray

Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays.

Example 1:

Input: nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
Output: 3
Explanation: The repeated subarray with maximum length is [3,2,1].

Example 2:

Input: nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
Output: 5
Explanation: The repeated subarray with maximum length is [0,0,0,0,0].

思路:

image.png

步骤:

  1. dp[i] 的意义: 表示最大相同序列长度
  2. 递推公式:
  dp[i][j] = dp[i - 1][j - 1] + 1;
  1. 初始化: 二维数组初始化为0
  2. 遍历顺序: 双层顺序遍历
var findLength = function(nums1, nums2) {
    let m = nums1.length;
    let n = nums2.length;

    let dp = Array(m + 1).fill(0).map(() => Array(n + 1).fill(0));

    let res = 0;

    for (let i = 1; i <= m; i++) {
        for (let j = 1; j <= n; j++) {
            if (nums1[i - 1] === nums2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }

            res = Math.max(dp[i][j], res);
        }
    }

    return res;
};

时间复杂度: O(mn) 空间复杂度: O(mn)