Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
Example
A = [1, 2, 3, 4]
return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
Solution
# not exactly dp
class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
cur, num = 0, 0
for i in range(2,len(A)):
if A[i]-A[i-1]==A[i-1]-A[i-2]:
cur += 1
num += cur
else:
cur = 0
return num