-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path1027.Longest Arithmetic Subsequence.cpp
51 lines (44 loc) · 1.16 KB
/
1027.Longest Arithmetic Subsequence.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
/*
written by Pankaj Kumar.
country:-INDIA
*/
typedef long long ll;
const ll INF = 1e18;
const ll mod1 = 1e9 + 7;
const ll mod2 = 998244353;
// Add main code here
class Solution
{
public:
int longestArithSeqLength(vector<int> &nums)
{
int n = nums.size();
if (n <= 2)
{
return n;
}
// Create a vector of unordered maps
vector<unordered_map<int, int>> dp(n);
int maxLength = 2;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
int diff = nums[i] - nums[j];
// If the difference exists in the subsequence ending at index j
// Extend the subsequence and update the length in dp[i]
if (dp[j].find(diff) != dp[j].end())
{
dp[i][diff] = dp[j][diff] + 1;
}
else
{
// If the difference doesn't exist, initialize it to 2
dp[i][diff] = 2;
}
maxLength = max(maxLength, dp[i][diff]);
}
}
return maxLength;
}
};