> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Length of Repeated Subarray

> Tested Python solution for LeetCode 718 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 718, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Dynamic Programming](/catalog/topics/dynamic-programming), [Sliding Window](/catalog/topics/sliding-window), Rolling Hash, [Hash Function](/catalog/topics/hash-function). [View on LeetCode](https://leetcode.com/problems/maximum-length-of-repeated-subarray/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 718   # by problem number
lcpy gen -s maximum_length_of_repeated_subarray   # by problem name
```

## Problem

Given two integer arrays `nums1` and `nums2`, return *the maximum length of a subarray that appears in **both** arrays*.

### Examples

```
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].
```

```
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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_length_of_repeated_subarray/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_length_of_repeated_subarray/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(len(nums1) * len(nums2))
    # Space: O(len(nums2))
    def find_length(self, nums1: list[int], nums2: list[int]) -> int:
        best = 0
        prev = [0] * (len(nums2) + 1)
        for a in nums1:
            cur = [0] * (len(nums2) + 1)
            for j, b in enumerate(nums2, start=1):
                if a == b:
                    cur[j] = prev[j - 1] + 1
                    if cur[j] > best:
                        best = cur[j]
            prev = cur
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(len(nums1) \* len(nums2)) | O(len(nums2)) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.