python solution with explanation. 90+


  • 0
    F

    #Try to find the pattern
    #Take the following eg
    input:
    [1, 2, 3, 4, 6, 7, 8, 10, 12, 14, 16]
    no. of size 3 slices so fat while iterating left to right:
    [0, 0, 1, 2, 0, 0, 1, 0, 1, 2, 3]

    now total number of slices: 1 + 2 + 1 + 1 + 2 + 3 = 10

    class Solution(object):
    def numberOfArithmeticSlices(self, A):
    prev = 0
    sums = 0

        i = 2
        while i<len(A):
            if A[i] - A[i-1] == A[i-1] - A[i-2]: 
               prev += 1
               sums += prev
            else:
                prev = 0
            i+= 1
        return sums

Log in to reply
 

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