Skip to content

Commit 22051ea

Browse files
committed
Time: 415 ms (86.33%), Space: 44.5 MB (96.04%) - LeetHub
1 parent ca8b094 commit 22051ea

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import defaultdict
2+
from typing import List
3+
4+
5+
class Solution:
6+
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
7+
n = len(nums)
8+
totalCount = 0
9+
10+
dp = [defaultdict(int) for _ in range(n)]
11+
12+
for i in range(1, n):
13+
for j in range(i):
14+
diff = nums[i] - nums[j]
15+
16+
if diff < -2**31 or diff > 2**31 - 1:
17+
continue
18+
19+
count = dp[j][diff] if diff in dp[j] else 0
20+
21+
totalCount += count
22+
dp[i][diff] += count + 1
23+
24+
return totalCount

0 commit comments

Comments
 (0)