54
54
55
55
** 方法一:动态规划**
56
56
57
+ 我们定义 $f[ i] [ j ] $ 表示以 $nums[ i] $ 结尾且公差为 $j$ 的等差数列的最大长度。初始时 $f[ i] [ j ] =1$,即每个元素自身都是一个长度为 $1$ 的等差数列。
58
+
59
+ 考虑 $f[ i] [ j ] $,我们可以枚举 $nums[ i] $ 的前一个元素 $nums[ k] $,那么 $d=nums[ i] -nums[ k] +500$,此时有 $f[ i] [ j ] =\max(f[ i] [ j ] , f[ k] [ j ] +1)$,然后我们更新答案 $ans=\max(ans, f[ i] [ j ] )$。
60
+
61
+ 最后返回答案即可。
62
+
63
+ 时间复杂度 $O(n \times d)$,空间复杂度 $O(n \times d)$。其中 $n$ 和 $d$ 分别是数组 $nums$ 的长度以及数组 $nums$ 中元素的最大值与最小值的差值。
64
+
57
65
<!-- tabs:start -->
58
66
59
67
### ** Python3**
64
72
class Solution :
65
73
def longestArithSeqLength (self , nums : List[int ]) -> int :
66
74
n = len (nums)
67
- dp = [[1 ] * 1001 for _ in range (n)]
75
+ f = [[1 ] * 1001 for _ in range (n)]
68
76
ans = 0
69
77
for i in range (1 , n):
70
- for j in range (i):
71
- d = nums[i] - nums[j ] + 500
72
- dp [i][d ] = max (dp [i][d ], dp[j][d ] + 1 )
73
- ans = max (ans, dp [i][d ])
78
+ for k in range (i):
79
+ j = nums[i] - nums[k ] + 500
80
+ f [i][j ] = max (f [i][j ], f[k][j ] + 1 )
81
+ ans = max (ans, f [i][j ])
74
82
return ans
75
83
```
76
84
@@ -83,12 +91,12 @@ class Solution {
83
91
public int longestArithSeqLength (int [] nums ) {
84
92
int n = nums. length;
85
93
int ans = 0 ;
86
- int [][] dp = new int [n][1001 ];
94
+ int [][] f = new int [n][1001 ];
87
95
for (int i = 1 ; i < n; ++ i) {
88
- for (int j = 0 ; j < i; ++ j ) {
89
- int d = nums[i] - nums[j ] + 500 ;
90
- dp [i][d ] = Math . max(dp [i][d ], dp[j][d ] + 1 );
91
- ans = Math . max(ans, dp [i][d ]);
96
+ for (int k = 0 ; k < i; ++ k ) {
97
+ int j = nums[i] - nums[k ] + 500 ;
98
+ f [i][j ] = Math . max(f [i][j ], f[k][j ] + 1 );
99
+ ans = Math . max(ans, f [i][j ]);
92
100
}
93
101
}
94
102
return ans + 1 ;
@@ -103,16 +111,17 @@ class Solution {
103
111
public:
104
112
int longestArithSeqLength(vector<int >& nums) {
105
113
int n = nums.size();
114
+ int f[ n] [ 1001 ] ;
115
+ memset(f, 0, sizeof(f));
106
116
int ans = 0;
107
- vector<vector<int >> dp(n, vector<int >(1001, 1));
108
117
for (int i = 1; i < n; ++i) {
109
- for (int j = 0; j < i; ++j ) {
110
- int d = nums[ i] - nums[ j ] + 500;
111
- dp [ i] [ d ] = max(dp [ i] [ d ] , dp [ j ] [ d ] + 1);
112
- ans = max(ans, dp [ i] [ d ] );
118
+ for (int k = 0; k < i; ++k ) {
119
+ int j = nums[ i] - nums[ k ] + 500;
120
+ f [ i] [ j ] = max(f [ i] [ j ] , f [ k ] [ j ] + 1);
121
+ ans = max(ans, f [ i] [ j ] );
113
122
}
114
123
}
115
- return ans;
124
+ return ans + 1 ;
116
125
}
117
126
};
118
127
```
@@ -122,16 +131,16 @@ public:
122
131
```go
123
132
func longestArithSeqLength(nums []int) int {
124
133
n := len(nums)
125
- dp := make([][]int, n)
126
- for i := range dp {
127
- dp [i] = make([]int, 1001)
134
+ f := make([][]int, n)
135
+ for i := range f {
136
+ f [i] = make([]int, 1001)
128
137
}
129
138
ans := 0
130
139
for i := 1; i < n; i++ {
131
- for j := 0; j < i; j ++ {
132
- d := nums[i] - nums[j ] + 500
133
- dp [i][d ] = max(dp [i][d ], dp[j][d ]+1)
134
- ans = max(ans, dp [i][d ])
140
+ for k := 0; k < i; k ++ {
141
+ j := nums[i] - nums[k ] + 500
142
+ f [i][j ] = max(f [i][j ], f[k][j ]+1)
143
+ ans = max(ans, f [i][j ])
135
144
}
136
145
}
137
146
return ans + 1
0 commit comments