LeetCode 300. Longest Increasing Subsequence
Problem
Given an integer array nums
, return the length of the longest strictly increasing subsequence.
A subsequence is a sequence that can be derived from an array by deleting some or no elements 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], 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
-104 <= nums[i] <= 104
Follow up: Can you come up with an algorithm that runs in O(n log(n))
time complexity?
翻译
给定一个整数数组 nums
,返回最长的严格递增子序列的长度。
题目分析
- 本题是一道典型的动态规划题目,我们可以使用动态规划的思想来解决这道题目。
- 我们可以使用一个一维数组来存储以
nums[i]
结尾的最长严格递增子序列的长度,dp[i]
表示以nums[i]
结尾的最长严格递增子序列的长度。 - 对于
nums[i]
,如果nums[i] > nums[j]
,那么dp[i] = max(dp[i], dp[j] + 1)
,其中0 <= j < i
。 - 我们可以使用一个变量来记录最长的严格递增子序列的长度,这样就可以得到最终的结果。
- 时间复杂度为
O(n^2)
,空间复杂度为O(n)
。
代码
python
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
n = len(nums)
dp = [1] * n
res = 1
for i in range(1, n):
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
res = max(res, dp[i])
return res
cpp
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, 1);
int res = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
res = max(res, dp[i]);
}
return res;
}
};
LeetCode 718. Maximum Length of Repeated Subarray
Problem
Given two integer arrays nums1
and nums2
, return the maximum length of a subarray that appears in both arrays.
翻译
给定两个整数数组 nums1
和 nums2
,返回两个数组中公共子数组的最大长度。
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
Constraints:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 100
题目分析
- 本题是一道典型的动态规划题目,我们可以使用动态规划的思想来解决这道题目。
- 我们可以使用一个二维数组来存储两个数组中公共子数组的长度,
dp[i][j]
表示以nums1[i]
和nums2[j]
结尾的公共子数组的长度。 - 如果
nums1[i] == nums2[j]
,那么dp[i][j] = dp[i-1][j-1] + 1
,否则dp[i][j] = 0
。 - 我们可以使用一个变量来记录最大的公共子数组的长度,这样就可以得到最终的结果。
- 时间复杂度为
O(n^2)
,空间复杂度为O(n^2)
。
代码
python
class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
m, n = len(nums1), len(nums2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
res = 0
for i in range(1, m + 1):
for j in range(1, n + 1):
if nums1[i - 1] == nums2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
res = max(res, dp[i][j])
return res
cpp
class Solution {
public:
int findLength(vector<int>& nums1, vector<int>& nums2) {
int m = nums1.size(), n = nums2.size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
int res = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (nums1[i - 1] == nums2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
res = max(res, dp[i][j]);
}
}
}
return res;
}
};