Journey of LeetCode|DAY 45
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;
}
};