leetcode 300. Longest increasing subsequence
Topic link: 300. The longest increasing subsequence - LeetCode
topic overview
Given an array of integers nums, find the length of the longest strictly increasing subsequence in it.
A subsequence is a sequence derived from an array in which elements are removed (or not) without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
Example 1:
Input: nums = [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], so 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
train of thought
1. Determine the meaning of the dp array
dp[i]: the length of the longest increasing subsequence ending with nums[i] before i (including i)
2. Determine the recursive formula
dp[i] = max(dp[i], dp[j] + 1) (the condition to execute this code requires nums[i] > nums[j])
3. Array initialization
dp[i]=1
4. Determine the traversal order
front to back
5. Print dp array
Code
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
if(nums.size() <= 1) return nums.size();
vector<int> dp(nums.size(),1);
int result = 0;
for(int i = 1;i < nums.size();i++) {
for(int j = 0;j < i;j++) {
if(nums[i] > nums[j]) dp[i] = max(dp[j] + 1,dp[i]);
}
if(dp[i] > result) result = dp[i];
}
return result;
}
};
leetcode 674. Longest Continuous Increasing Sequence
Topic link: 674. The longest continuous increasing sequence - LeetCode
topic overview
Given an unsorted array of integers, find the longest continuously increasing subsequence and return the length of that sequence.
A continuously increasing subsequence can be determined by two subscripts l and r (l < r), if for each l <= i < r, there are nums[i] < nums[i + 1], then the subsequence [ nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] are continuously increasing subsequences.
Example 1:
Input: nums = [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing sequence is [1,3,5], with a length of 3. Although [1,3,5,7] is also an ascending subsequence, it is not continuous because 5 and 7 are separated by 4 in the original array.
Example 2:
Input: nums = [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing sequence is [2], with a length of 1.
train of thought
The difference between this question and the previous question is that this question is continuous , so the recursive formula will be different. This question only needs to compare the current element with the previous element, and there is no need to apply another layer of for loop.
The recursive formula for this question: dp[i] = dp[i - 1] + 1
Code
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
if(nums.size() == 0) return 0;
int result = 1;
vector<int> dp(nums.size(),1);
for(int i = 1;i < nums.size();i++) {
if(nums[i] > nums[i - 1]) {
dp[i] = dp[i - 1] + 1;
}
if(dp[i] > result) result = dp[i];
}
return result;
}
};
leetcode 718. Longest Repeating Subarray
Topic Link: 718. Longest Repeating Subarray - LeetCode
topic overview
Given two integer arrays A and B , return the length of the longest subarray common to both arrays.
Example:
enter:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
Output: 3
Explanation: The longest common subarray is [3, 2, 1].
train of thought
1. Determine the meaning of the dp array
dp[i][j] : A that ends with subscript i - 1, and B that ends with subscript j - 1, and the length of the longest repeated subarray is dp[i][j].
( Special attention : "A that ends with i - 1" indicates a string that must end with A[i-1])
Define dp[i][j] as A ending with subscript i, and B ending with subscript j. The length of the longest repeated subarray is also acceptable, but it is troublesome to implement
2. Determine the recursive formula
dp[i][j] = dp[i - 1][j - 1] + 1 (the condition required to execute this code is: when A[i - 1] and B[j - 1] are equal)
3. Array initialization
dp[i][0] and dp[0][j] are initialized to 0
4. Determine the traversal order
The order of the two layers of for loops can be reversed, and it doesn't matter which one is traversed first.
5. Print the array
Code
class Solution {
public:
int findLength(vector<int>& nums1, vector<int>& nums2) {
vector<vector<int>> dp(nums1.size() + 1,vector<int>(nums2.size() + 1,0));
int result = 0;
for(int i = 1;i <= nums1.size();i++) {
for(int j = 1;j <= nums2.size();j++) {
if(nums1[i - 1] == nums2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
if(dp[i][j] > result) result = dp[i][j];
}
}
return result;
}
};