forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1027.go
More file actions
27 lines (26 loc) · 657 Bytes
/
Copy path1027.go
File metadata and controls
27 lines (26 loc) · 657 Bytes
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
func longestArithSeqLength(A []int) int {
n, res, cM := len(A), 0, 10000
idx := make([]int, cM + 1)
for i, _ := range idx {
idx[i] = -1
}
f := make([][]int, n)
for i := 0; i < n; i++ {
f[i] = make([]int, n)
}
for i := 0; i < n - 1; i++ {
for j := i + 1; j < n; j++ {
pre := 2 * A[i] - A[j]
if pre >= 0 && pre <= cM && idx[pre] != -1 {
f[i][j] = f[idx[pre]][i] + 1
} else {
f[i][j] = 2
}
if res < f[i][j] {
res = f[i][j]
}
}
idx[A[i]] = i
}
return res
}