Advertisement

Maximum Length of Repeated Subarray - LeetCode 718 Solution

Maximum Length of Repeated Subarray - Complete Solution Guide

Maximum Length of Repeated Subarray is LeetCode problem 718, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem asks us to find the length of the longest subarray that is common to two given integer arrays, `nums1` and `nums2`. A subarray is a contiguous sequence of elements within an array. The task is to identify the subarray that appears in both input arrays and has the maximum possible length, then return that length. For example, if `nums1` is `[1,2,3,2,1]` and `nums2` is `[3,2,1,4,7]`, the longest common subarray is `[3,2,1]` with a length of 3. The constraints are that the lengths of the input arrays are between 1 and 1000, and the elements within the arrays are between 0 and 100.

Solution Approach

The solution uses dynamic programming to find the maximum length of the repeated subarray. A 2D array `dp` is created with dimensions `(m+1) x (n+1)`, where `m` and `n` are the lengths of `nums1` and `nums2`, respectively. `dp[i][j]` stores the length of the longest common subarray ending at indices `i-1` in `nums1` and `j-1` in `nums2`. The algorithm iterates through the `dp` array, and if `nums1[i-1]` is equal to `nums2[j-1]`, it means we have found a match, so we increment the length of the common subarray by 1, using the value from the previous diagonal element (`dp[i-1][j-1]`). The maximum length found during this process is then returned.

Step-by-Step Algorithm

  1. Step 1: Initialize a 2D DP table `dp` of size `(m+1) x (n+1)` with all elements set to 0. `m` and `n` are the lengths of `nums1` and `nums2` respectively.
  2. Step 2: Initialize a variable `max_length` to 0. This variable will store the maximum length of the common subarray found so far.
  3. Step 3: Iterate through the `dp` table, starting from index (1, 1) up to (m, n).
  4. Step 4: For each cell `dp[i][j]`, check if `nums1[i-1]` is equal to `nums2[j-1]`.
  5. Step 5: If `nums1[i-1] == nums2[j-1]`, then set `dp[i][j] = dp[i-1][j-1] + 1`. This means we extend the common subarray by 1.
  6. Step 6: Update `max_length` to be the maximum of its current value and `dp[i][j]`.
  7. Step 7: After iterating through the entire `dp` table, return `max_length`.

Key Insights

  • Insight 1: Dynamic programming is well-suited for this problem because it allows us to build up solutions for smaller subproblems (smaller subarrays) and use them to find the solution for larger subproblems.
  • Insight 2: The core idea is to use a 2D DP table where `dp[i][j]` stores the length of the longest common subarray ending at index `i-1` in `nums1` and index `j-1` in `nums2`.
  • Insight 3: If `nums1[i-1]` and `nums2[j-1]` are equal, then we extend the common subarray by 1. Otherwise, the length of the common subarray ending at `i-1` and `j-1` is 0.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Topics

This problem involves: Array, Binary Search, Dynamic Programming, Sliding Window, Rolling Hash, Hash Function.

Companies

Asked at: Citadel.