Skip to content

Commit 9330f0d

Browse files
add 446
1 parent 6cf23a6 commit 9330f0d

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Your ideas/fixes/algorithms are more than welcome!
170170
|449|[Serialize and Deserialize BST](https://leetcode.com/problems/serialize-and-deserialize-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_449.java)| O(n)|O(h) | Medium| BFS
171171
|448|[Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_448.java)| O(n)|O(1) | Easy| Array, HashMap
172172
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_447.java)| O(n^2)|O(n) | Easy| HashMap
173+
|446|[Arithmetic Slices II - Subsequence](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_446.java)| O(n^2)|O(n^2) | Hard| DP
173174
|445|[Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_445.java)| O(max(m,n)|O(max(m,n)) | Medium| Stack, LinkedList
174175
|444|[Sequence Reconstruction](https://leetcode.com/problems/sequence-reconstruction/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_444.java)| O(n)|O(n) | Medium| Topological Sort, Graph
175176
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_442.java)| O(n)|O(1) | Medium| Array
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 446. Arithmetic Slices II - Subsequence
8+
*
9+
* A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
10+
11+
For example, these are arithmetic sequences:
12+
13+
1, 3, 5, 7, 9
14+
7, 7, 7, 7
15+
3, -1, -5, -9
16+
17+
The following sequence is not arithmetic.
18+
19+
1, 1, 2, 5, 7
20+
21+
A zero-indexed array A consisting of N numbers is given. A subsequence slice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0 < P1 < ... < Pk < N.
22+
23+
A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2.
24+
25+
The function should return the number of arithmetic subsequence slices in the array A.
26+
27+
The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1.
28+
29+
30+
Example:
31+
32+
Input: [2, 4, 6, 8, 10]
33+
34+
Output: 7
35+
36+
Explanation:
37+
All arithmetic subsequence slices are:
38+
[2,4,6]
39+
[4,6,8]
40+
[6,8,10]
41+
[2,4,6,8]
42+
[4,6,8,10]
43+
[2,4,6,8,10]
44+
[2,6,10]
45+
*/
46+
public class _446 {
47+
/**reference: https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution*/
48+
public int numberOfArithmeticSlices(int[] A) {
49+
int res = 0;
50+
Map<Integer, Integer>[] map = new Map[A.length];
51+
52+
for (int i = 0; i < A.length; i++) {
53+
map[i] = new HashMap<>(i);
54+
55+
for (int j = 0; j < i; j++) {
56+
long diff = (long) A[i] - A[j];
57+
if (diff <= Integer.MIN_VALUE || diff > Integer.MAX_VALUE) continue;
58+
59+
int d = (int) diff;
60+
int c1 = map[i].getOrDefault(d, 0);
61+
int c2 = map[j].getOrDefault(d, 0);
62+
res += c2;
63+
map[i].put(d, c1 + c2 + 1);
64+
}
65+
}
66+
67+
return res;
68+
}
69+
}

0 commit comments

Comments
 (0)