718. Maximum Length of Repeated Array¶
Edited by Charles
Medium · Python · Dynamic Programming
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Given two integer arrays nums1 and nums2, return the length of the longest common contiguous subarray.
Unlike the Longest Common Subsequence (LCS) problem, the matching elements in this problem must be contiguous in both arrays.
Original study notes follow, including intermediate snippets and earlier attempts.
Idea¶
Given two integer arrays nums1 and nums2, return the length of the longest common contiguous subarray.
Unlike the Longest Common Subsequence (LCS) problem, the matching elements in this problem must be contiguous in both arrays.
For example:
The longest common subarray is:
Therefore, the answer is 3.
Dynamic Programming¶
State Definition¶
Let
denote:
The length of the longest common subarray ending at
nums1[i - 1]andnums2[j - 1].
The keyword "ending at" is crucial because the subarray must be continuous.
For example:
The DP table will eventually contain:
Initialization¶
If either array is empty, there is no common subarray.
Therefore, initialize the first row and first column to 0.
The extra row and column eliminate boundary checks when accessing dp[i - 1][j - 1].
State Transition¶
If the current elements are equal,
then the common subarray can be extended by one element.
Update:
Otherwise,
because the subarray must be continuous, and a mismatch breaks the current subarray.
Transition equation:
The key idea is:
A common subarray can only be extended when the current elements are equal. Otherwise, the current common subarray ends immediately.
Why Can't We Return dp[-1][-1]?¶
The longest common subarray does not necessarily end at the last elements of both arrays.
For example,
The DP table contains:
The longest common subarray is
with length 2, but
Therefore, we must maintain a global maximum throughout the DP process.
Example¶
Initialization:
Match: nums1[2] == nums2[0]¶
Update:
Match: nums1[3] == nums2[1]¶
Update:
Match: nums1[4] == nums2[2]¶
Update:
The longest common subarray becomes
Final answer:
Complexity Analysis¶
Time Complexity
where
m = len(nums1)n = len(nums2)
Each pair of elements is processed exactly once.
Space Complexity
A 2D DP table is required.
Note: Since each state only depends on the upper-left diagonal element, the DP table can be optimized to O(n) using a rolling array.
Summary¶
State Definition
Initialization
State Transition
Traversal Order
Traverse both arrays from left to right while filling the DP table.
Final Answer
Maintain a global maximum during the DP process.
Common Mistakes¶
- This problem asks for the Longest Common Subarray, not the Longest Common Subsequence (LCS). The matched elements must be contiguous.
dp[i][j]represents the longest common subarray ending atnums1[i-1]andnums2[j-1].- When two elements are different,
instead of taking the maximum of neighboring states, because the current common subarray is broken.
- Do not return
dp[-1][-1], since the longest common subarray may end anywhere in the two arrays. Maintain a global maximum instead.
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
Python solution¶
class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
l1 = len(nums1)
l2 = len(nums2)
dp = [[0] * (l2 + 1) for _ in range(l1 + 1)]
res = 0
for i in range(1, l1 + 1):
for j in range(1, l2 + 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