-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path413 Arithmetic Slices.py
47 lines (35 loc) · 1.09 KB
/
413 Arithmetic Slices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python3
"""
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.
The function should return the number of arithmetic slices in the array A.
Author: Rajeev Ranjan
"""
class Solution:
def count(self, l):
return (l-1) * l // 2
def numberOfArithmeticSlices(self, A):
"""
Diff the array, find the pattern.
Find that it is a function of length of the sequence
With 3 consecutive sequence (l - 1) * l / 2
:type A: List[int]
:rtype: int
"""
ret = 0
if len(A) < 3:
return ret
delta = []
for i in range(1, len(A)):
delta.append(A[i] - A[i-1])
s = 0
e = 0
while s < len(delta):
while e < len(delta) and delta[s] == delta[e]:
e += 1
l = e - s
ret += self.count(l)
s = e
return ret
if __name__ == "__main__":
assert Solution().numberOfArithmeticSlices([1, 2, 3, 4]) == 3