-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArithmeticSlicesII-Subsequence3.cpp
58 lines (47 loc) · 1.24 KB
/
ArithmeticSlicesII-Subsequence3.cpp
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
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<vector>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
using namespace std;
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
unordered_map<long, unordered_map<long, int> > needDiffCount;
unordered_multiset<int> s(A.begin(), A.end());
int result = 0;
for(int i = 1; i < A.size(); i++) {
if(needDiffCount.count(A[i])) {
for(auto& diffCount : needDiffCount[A[i]]) {
result += diffCount.second;
long need = A[i] + diffCount.first;
if(s.count(need)) {
needDiffCount[need][diffCount.first] += diffCount.second;
}
}
}
for(int j = 0; j < i; j++) {
long diff = (long)A[i] - A[j];
if(diff < INT_MIN || diff > INT_MAX) continue;
long need = A[i] + diff;
if(need < INT_MIN || need > INT_MAX || s.count(need) == 0) continue;
needDiffCount[need][diff]++;
}
s.erase(s.find(A[i]));
}
return result;
}
};
int main() {
int n;
cin>>n;
vector<int> nums;
for(int i = 0; i < n; i++) {
int num;
cin>>num;
nums.push_back(num);
}
Solution *solution = new Solution();
cout<<solution->numberOfArithmeticSlices(nums);
return 0;
}