Python DP Solution (28ms)


  • 0
    B
    class Solution(object):
        def numberOfArithmeticSlices(self, A):
            n = len(A)
            if n < 3: return 0
            dp = [None] * n
            dp[0] = dp[1] = 0
            curr, diff, l = A[0]-A[1], 0, 2    
            for i in range(1,n-1):
                diff = A[i]-A[i+1]
                if curr == diff:
                    l += 1
                    dp[i+1] = dp[i]+(l-3)+1 if l >= 3 else dp[i]
                else:
                    dp[i+1] = dp[i]
                    curr, l = diff, 2
            return dp[-1]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.