mirror of https://github.com/doocs/leetcode.git
14 lines
438 B
TypeScript
14 lines
438 B
TypeScript
function longestArithSeqLength(nums: number[]): number {
|
|
const n = nums.length;
|
|
let ans = 0;
|
|
const f: number[][] = Array.from({ length: n }, () => new Array(1001).fill(0));
|
|
for (let i = 1; i < n; ++i) {
|
|
for (let k = 0; k < i; ++k) {
|
|
const j = nums[i] - nums[k] + 500;
|
|
f[i][j] = Math.max(f[i][j], f[k][j] + 1);
|
|
ans = Math.max(ans, f[i][j]);
|
|
}
|
|
}
|
|
return ans + 1;
|
|
}
|