LeetCode DAY45(300.Longest Increasing Subsequence&674.Longest Continuous IncreasingSubsequence&718)

本文介绍了使用动态规划方法解决三个LeetCode题目:300.最长递增子序列、674.最长连续递增子序列和718.最大重复子数组。分别给出了C++实现的解决方案,涉及严格递增和连续递增序列的长度计算以及两个数组间重复子数组的最大长度。
摘要由CSDN通过智能技术生成

Preface

This is a new day to continue my Dynamic Programming journey.
Learn something new and keep reviewing what I learnt before.

1. Longest Increasing Subsequence

LeetCode Link: 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

Constraints:

1 <= nums.length <= 2500
-10^4 <= nums[i] <= 10^4

Analysis and Solution

Dynamic Programming

LeetCode C++ as followings Dynamic Programming

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        if (nums.size() <= 1) return nums.size();//remove case 0
        vector<int> dp(nums.size(), 1);
        int result = 0;
        for (int i = 1; i < nums.size(); i++) {//loop i
            for (int j = 0; j < i; j++) {//loop j
                if (nums[i] > nums[j]) dp[i] = max(dp[i], dp[j] + 1);//get the maxValue of dp[i] and dp[j]
            }
            if (dp[i] > result) result = dp[i]; // get the longest subsequence
        }
        return result;
    }
};

2. Longest Continuous Increasing Subsequence

LeetCode Link: 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.

A 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 < r, nums[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.

Constraints:

1 <= nums.length <= 10^4
-10^9 <= nums[i] <= 10^9

Analysis and Solution

Dynamic Programming

LeetCode C++ as followings Dynamic Programming

class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        if (nums.size() == 0) return 0;//remove case 0
        int result = 1;
        vector<int> dp(nums.size() ,1);
        for (int i = 1; i < nums.size(); i++) {
            if (nums[i] > nums[i - 1]) { //  continuous recording 
                dp[i] = dp[i - 1] + 1;//length +1
            }
            if (dp[i] > result) result = dp[i];//get the result
        }
        return result;
    }
};

3. Maximum Length of Repeated Subarray

LeetCode Link: 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].

Constraints:

1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 100

Analysis and Solution

Dynamic Programming

LeetCode C++ as followings Dynamic Programming

class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        vector<vector<int>> dp (nums1.size() + 1, vector<int>(nums2.size() + 1, 0));//two-dimensional array
        int result = 0;
        for (int i = 1; i <= nums1.size(); i++) {//loop i
            for (int j = 1; j <= nums2.size(); j++) {//loop j
                if (nums1[i - 1] == nums2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;//Recursive formula
                }
                if (dp[i][j] > result) result = dp[i][j];
            }
        }
        return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值