mediumDynamic Programming 2DPure DSA~20 min
Longest Common Contiguous Run in Two Logs
Two event logs are compared to find their longest identical contiguous segment — useful for detecting a shared replayed burst. Return the length of that maximal matching run.
Problem
Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays (contiguous in each).
Input
Two integer arrays nums1 and nums2.
Output
An integer: the length of the longest common subarray.
Constraints
- 1 <= nums1.length, nums2.length <= 1000
- 0 <= nums1[i], nums2[j] <= 100
Examples
Example 1
Input
nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
Output
3
The run [3,2,1] is common to both.
Example 2
Input
nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
Output
5
The entire arrays match.