Simple Python solution

  • 0

    An arithmetic slice is equivalent to a zero in second order derivative therefore the number of arithmetic slices is equivalent to the partial sum of consecutive zeros.

    class Solution(object):
        def numberOfArithmeticSlices(self, A):
            :type A: List[int]
            :rtype: int
            sndOrder = self.getGradient(self.getGradient(A))
            res, zeros = 0, 0
            for n in sndOrder:
                zeros = zeros + 1 if not n else 0
                res += zeros
            return res
        def getGradient(self, arr):
            return [t - s for s, t in zip(arr, arr[1:])]

Log in to reply

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