-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountLongestArithmeticSequence.cpp
More file actions
46 lines (38 loc) · 1.18 KB
/
CountLongestArithmeticSequence.cpp
File metadata and controls
46 lines (38 loc) · 1.18 KB
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
#include <iostream>
#include <vector>
#include <unordered_map>
#include <utility>
using namespace std;
int countLongestArithmeticSequence(vector<int> &nums) {
// mapping from value difference to index pair
unordered_map<int, vector<pair<int, int> > > diffMap;
for(int i = 0; i < nums.size() - 1; ++i) {
for(int j = i+1; j < nums.size(); ++j) {
int diff = nums[i] - nums[j];
diffMap[diff].push_back(make_pair(i, j));
}
}
int longest = 0;
unordered_map<int, vector<pair<int, int> > >::iterator it;
for(it = diffMap.begin(); it != diffMap.end(); ++it) {
vector<int> lens(nums.size(), 1); // init all the numbers in the pairs
// doesn't matter also initlizing the nums that are not int the pair
vector<pair<int, int> > pairs = it->second;
for(int i = 0; i < pairs.size(); ++i) {
lens[pairs[i].second] = max(lens[pairs[i].second], lens[pairs[i].first]+1);
longest = max(longest, lens[pairs[i].second]);
}
}
return longest;
}
int main() {
vector<int> v;
v.push_back(1);
v.push_back(6);
v.push_back(3);
v.push_back(3);
v.push_back(5);
v.push_back(9);
v.push_back(7);
cout << countLongestArithmeticSequence(v) << endl;
}