Участник:Nik7/Arithmetic Slices II - Subsequence

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

«Arithmetic Slices II - Subsequence»

import collections
 
 
class Solution:
    def numberOfArithmeticSlices(self, A: List[int]) -> int:
        result = 0
        dp = collections.defaultdict(int)
        for i in range(1, len(A)):
            for j in range(i):
                diff = A[i] - A[j]
                dp[(i, diff)] += dp[(j, diff)] + 1
                result += dp[(j, diff)]
        return result