Skip to content

Commit 60c5358

Browse files
authored
Merge pull request larissalages#268 from Vedant-S/patch-5
Added Leetcode 413 Arithmetic Series Solution
2 parents 4533630 + f26ed50 commit 60c5358

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def numberOfArithmeticSlices(self, A: List[int]) -> int:
3+
4+
if len(A) < 3:
5+
return 0
6+
7+
dp = [0 for _ in range(len(A))]
8+
d = A[1] - A[0]
9+
start = 2
10+
11+
next_ = self._count(A, d, dp, start)
12+
while next_ < len(A):
13+
d = A[next_] - A[next_ - 1]
14+
start = next_ + 1
15+
next_ = self._count(A, d, dp, start)
16+
17+
return sum(dp)
18+
19+
def _count(self, A, d, dp, start):
20+
21+
for i in range(start, len(A)):
22+
if A[i] - A[i - 1] == d:
23+
dp[i] = dp[i - 1] + 1
24+
else:
25+
return i
26+
27+
return len(A)
28+
29+

0 commit comments

Comments
 (0)