1027. Longest Arithmetic Sequence
https://leetcode.com/problems/longest-arithmetic-sequence/
/*
* @lc app=leetcode id=1027 lang=cpp
*
* [1027] Longest Arithmetic Sequence
*/
// @lc code=start
class Solution {
public:
int longestArithSeqLength(vector<int>& A) {
const int N = A.size();
vector<unordered_map<int, int>> dp(N);
int res = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < i; ++j) {
const auto diff = A[i] - A[j];
if (dp[j].count(diff)) {
dp[i][diff] = dp[j][diff] + 1;
} else {
dp[i][diff] = 2;
}
res = max(res, dp[i][diff]);
}
}
return res;
}
};
// @lc code=end
Last updated