Taro Logo

Maximum Length of Repeated Subarray

Medium
Apple logo
Apple
Topics:
ArraysDynamic Programming

Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays. Let's explore different solutions for this problem, considering time and space complexity.

For example:

  1. nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7] should return 3 because the longest common subarray is [3,2,1].
  2. nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0] should return 5 since the entire array is a common subarray.

Consider these constraints:

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

Can you provide an efficient algorithm and analyze its time and space complexity? What are some edge cases to consider?

Solution


Brute Force Solution

A naive approach to solve this problem involves checking all possible subarrays in nums1 and nums2 and comparing them. We can iterate through all possible start indices and lengths in both arrays, comparing subarrays of the same length.

def findLength_brute_force(nums1, nums2):
    max_len = 0
    for i in range(len(nums1)):
        for j in range(len(nums2)):
            length = 0
            while i + length < len(nums1) and j + length < len(nums2) and nums1[i + length] == nums2[j + length]:
                length += 1
            max_len = max(max_len, length)
    return max_len

Time Complexity

The brute-force approach has a time complexity of O(m * n * min(m, n)), where m and n are the lengths of nums1 and nums2, respectively. This is because we potentially compare every possible subarray in nums1 with every possible subarray in nums2.

Space Complexity

The space complexity is O(1) as we only use a constant amount of extra space.

Optimal Solution: Dynamic Programming

A more efficient solution can be achieved using dynamic programming. We can create a 2D DP table where dp[i][j] stores the length of the longest common subarray ending at nums1[i-1] and nums2[j-1]. If nums1[i-1] and nums2[j-1] are equal, then dp[i][j] = dp[i-1][j-1] + 1. Otherwise, dp[i][j] = 0. We maintain a max_len variable to keep track of the maximum length found so far.

def findLength(nums1, nums2):
    n = len(nums1)
    m = len(nums2)
    dp = [[0] * (m + 1) for _ in range(n + 1)]
    max_len = 0

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            if nums1[i - 1] == nums2[j - 1]:
                dp[i][j] = dp[i - 1][j - 1] + 1
                max_len = max(max_len, dp[i][j])
            else:
                dp[i][j] = 0

    return max_len

Time Complexity

The dynamic programming solution has a time complexity of O(m * n), where m and n are the lengths of nums1 and nums2, respectively. This is because we iterate through each cell in the DP table once.

Space Complexity

The space complexity is O(m * n) due to the DP table.

Edge Cases

  1. Empty Arrays: If either nums1 or nums2 is empty, the maximum length of the common subarray is 0.
  2. Arrays with No Common Subarray: If there's no common subarray, the function should return 0.
  3. Arrays with Identical Elements: If the arrays have identical elements, the result is the length of the shorter array.
  4. Large Input Arrays: The code should be efficient enough to handle larger arrays (up to the constraint limits).