# Leetcode 718 - Max length of repeated subarray

`Note`

- DP question, it’s easier to understand it by drawing a table first. Take
`nums1 = [1,2,3,2,8], nums2 = [5,6,1,4,7]`

for example.- Use
`dp[i][j]`

to represent the max length of subarray that ends with`nums1[i]`

and`nums2[j]`

. - Initialize the
`first row`

and`first col`

because we need`i-1`

and`j-1`

in our code. - When
`nums1[i] === nums2[j]`

, there are two situations`nums1[i-1] !== nums2[j-1]`

so it’s a new subarray.`dp[i][j] = 1`

(need this because non first row/col is init as 0).`nums1[i-1] === nums2[j-1]`

so we just need to`add 1`

to`dp[i-1][j-1]`

.

- Use
- Use
`result`

to store`max`

while iterating.

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

1 | /** |