-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path718.py
25 lines (21 loc) · 869 Bytes
/
718.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Problem link: https://leetcode.com/problems/maximum-length-of-repeated-subarray/
class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
m = len(nums2)
# dp solution: dp[j] - length of the longest common subarray
# after processing 'i' elements from the first list and 'j' from the second
dp = [0] * m
ans = 0
for i in range(n):
newDp = [0] * m
for j in range(m):
# current elements are equal so try to make the common subarray longer
if nums1[i] == nums2[j]:
if j != 0:
newDp[j] = dp[j-1] + 1
else:
newDp[j] = 1
ans = max(ans, newDp[j])
dp = newDp
return ans