Skip to content

Commit e80f705

Browse files
Create 0446-arithmetic-slices-ii-subsequence.java
1 parent f5a9f86 commit e80f705

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public int numberOfArithmeticSlices(int[] nums) {
3+
int res = 0;
4+
int n = nums.length;
5+
6+
Map<Long, Integer>[] dp = new HashMap[n];
7+
for (int i = 0; i < n; i++) {
8+
dp[i] = new HashMap<>();
9+
}
10+
11+
for (int i = 0; i < n; i++) {
12+
for (int j = 0; j < i; j++) {
13+
long diff = (long) nums[i] - (long) nums[j];
14+
dp[i].put(diff, dp[i].getOrDefault(diff, 0) + 1 + dp[j].getOrDefault(diff, 0));
15+
res += dp[j].getOrDefault(diff, 0);
16+
}
17+
}
18+
19+
return res;
20+
}
21+
}

0 commit comments

Comments
 (0)