Участник:Kiranov dmitry/task4

Материал из DISCOPAL
Перейти к: навигация, поиск

ссылка:https://leetcode.com/problems/maximum-length-of-repeated-subarray/

 def compare(a, b):
    max1 = 0
    max2 = 0
    flag = 0
    for i in range(len(a)):
        if a[i] == b[i]:
            max1 += 1
            flag += 1
            if max1 > max2:
                    max2 = max1
        else:
            if flag > 0:
                flag = 0
                max1 = 0
    return max2
 
 
 class Solution:
    def findLength(self, A: List[int], B: List[int]) -> int:
        max1 = 0
        max2 = 0
        max3 = 0
        for i in range(len(A)):
            #print(A[i:], B[:len(B)-i])
            max1 = compare(A[i:], B[:len(B)-i])
            if max1 > max2:
                max2 = max1
        for i in range(len(A)):
            #print(B[i:], A[:len(B)-i])
            max1 = compare(B[i:], A[:len(A) - i])
            if max1 > max3:
                max3 = max1
        return max(max2, max3)