Skip to content

Commit 5c46255

Browse files
committed
feat: add solutions to lc problem: No.1027
No.1027.Longest Arithmetic Subsequence
1 parent 107746a commit 5c46255

File tree

6 files changed

+80
-69
lines changed

6 files changed

+80
-69
lines changed

solution/1000-1099/1027.Longest Arithmetic Subsequence/README.md

+32-23
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454

5555
**方法一:动态规划**
5656

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+
5765
<!-- tabs:start -->
5866

5967
### **Python3**
@@ -64,13 +72,13 @@
6472
class Solution:
6573
def longestArithSeqLength(self, nums: List[int]) -> int:
6674
n = len(nums)
67-
dp = [[1] * 1001 for _ in range(n)]
75+
f = [[1] * 1001 for _ in range(n)]
6876
ans = 0
6977
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])
7482
return ans
7583
```
7684

@@ -83,12 +91,12 @@ class Solution {
8391
public int longestArithSeqLength(int[] nums) {
8492
int n = nums.length;
8593
int ans = 0;
86-
int[][] dp = new int[n][1001];
94+
int[][] f = new int[n][1001];
8795
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]);
92100
}
93101
}
94102
return ans + 1;
@@ -103,16 +111,17 @@ class Solution {
103111
public:
104112
int longestArithSeqLength(vector<int>& nums) {
105113
int n = nums.size();
114+
int f[n][1001];
115+
memset(f, 0, sizeof(f));
106116
int ans = 0;
107-
vector<vector<int>> dp(n, vector<int>(1001, 1));
108117
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]);
113122
}
114123
}
115-
return ans;
124+
return ans + 1;
116125
}
117126
};
118127
```
@@ -122,16 +131,16 @@ public:
122131
```go
123132
func longestArithSeqLength(nums []int) int {
124133
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)
128137
}
129138
ans := 0
130139
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])
135144
}
136145
}
137146
return ans + 1

solution/1000-1099/1027.Longest Arithmetic Subsequence/README_EN.md

+24-23
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@
5656
class Solution:
5757
def longestArithSeqLength(self, nums: List[int]) -> int:
5858
n = len(nums)
59-
dp = [[1] * 1001 for _ in range(n)]
59+
f = [[1] * 1001 for _ in range(n)]
6060
ans = 0
6161
for i in range(1, n):
62-
for j in range(i):
63-
d = nums[i] - nums[j] + 500
64-
dp[i][d] = max(dp[i][d], dp[j][d] + 1)
65-
ans = max(ans, dp[i][d])
62+
for k in range(i):
63+
j = nums[i] - nums[k] + 500
64+
f[i][j] = max(f[i][j], f[k][j] + 1)
65+
ans = max(ans, f[i][j])
6666
return ans
6767
```
6868

@@ -73,12 +73,12 @@ class Solution {
7373
public int longestArithSeqLength(int[] nums) {
7474
int n = nums.length;
7575
int ans = 0;
76-
int[][] dp = new int[n][1001];
76+
int[][] f = new int[n][1001];
7777
for (int i = 1; i < n; ++i) {
78-
for (int j = 0; j < i; ++j) {
79-
int d = nums[i] - nums[j] + 500;
80-
dp[i][d] = Math.max(dp[i][d], dp[j][d] + 1);
81-
ans = Math.max(ans, dp[i][d]);
78+
for (int k = 0; k < i; ++k) {
79+
int j = nums[i] - nums[k] + 500;
80+
f[i][j] = Math.max(f[i][j], f[k][j] + 1);
81+
ans = Math.max(ans, f[i][j]);
8282
}
8383
}
8484
return ans + 1;
@@ -93,16 +93,17 @@ class Solution {
9393
public:
9494
int longestArithSeqLength(vector<int>& nums) {
9595
int n = nums.size();
96+
int f[n][1001];
97+
memset(f, 0, sizeof(f));
9698
int ans = 0;
97-
vector<vector<int>> dp(n, vector<int>(1001, 1));
9899
for (int i = 1; i < n; ++i) {
99-
for (int j = 0; j < i; ++j) {
100-
int d = nums[i] - nums[j] + 500;
101-
dp[i][d] = max(dp[i][d], dp[j][d] + 1);
102-
ans = max(ans, dp[i][d]);
100+
for (int k = 0; k < i; ++k) {
101+
int j = nums[i] - nums[k] + 500;
102+
f[i][j] = max(f[i][j], f[k][j] + 1);
103+
ans = max(ans, f[i][j]);
103104
}
104105
}
105-
return ans;
106+
return ans + 1;
106107
}
107108
};
108109
```
@@ -112,16 +113,16 @@ public:
112113
```go
113114
func longestArithSeqLength(nums []int) int {
114115
n := len(nums)
115-
dp := make([][]int, n)
116-
for i := range dp {
117-
dp[i] = make([]int, 1001)
116+
f := make([][]int, n)
117+
for i := range f {
118+
f[i] = make([]int, 1001)
118119
}
119120
ans := 0
120121
for i := 1; i < n; i++ {
121-
for j := 0; j < i; j++ {
122-
d := nums[i] - nums[j] + 500
123-
dp[i][d] = max(dp[i][d], dp[j][d]+1)
124-
ans = max(ans, dp[i][d])
122+
for k := 0; k < i; k++ {
123+
j := nums[i] - nums[k] + 500
124+
f[i][j] = max(f[i][j], f[k][j]+1)
125+
ans = max(ans, f[i][j])
125126
}
126127
}
127128
return ans + 1

solution/1000-1099/1027.Longest Arithmetic Subsequence/Solution.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ class Solution {
22
public:
33
int longestArithSeqLength(vector<int>& nums) {
44
int n = nums.size();
5+
int f[n][1001];
6+
memset(f, 0, sizeof(f));
57
int ans = 0;
6-
vector<vector<int>> dp(n, vector<int>(1001, 1));
78
for (int i = 1; i < n; ++i) {
8-
for (int j = 0; j < i; ++j) {
9-
int d = nums[i] - nums[j] + 500;
10-
dp[i][d] = max(dp[i][d], dp[j][d] + 1);
11-
ans = max(ans, dp[i][d]);
9+
for (int k = 0; k < i; ++k) {
10+
int j = nums[i] - nums[k] + 500;
11+
f[i][j] = max(f[i][j], f[k][j] + 1);
12+
ans = max(ans, f[i][j]);
1213
}
1314
}
14-
return ans;
15+
return ans + 1;
1516
}
1617
};

solution/1000-1099/1027.Longest Arithmetic Subsequence/Solution.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
func longestArithSeqLength(nums []int) int {
22
n := len(nums)
3-
dp := make([][]int, n)
4-
for i := range dp {
5-
dp[i] = make([]int, 1001)
3+
f := make([][]int, n)
4+
for i := range f {
5+
f[i] = make([]int, 1001)
66
}
77
ans := 0
88
for i := 1; i < n; i++ {
9-
for j := 0; j < i; j++ {
10-
d := nums[i] - nums[j] + 500
11-
dp[i][d] = max(dp[i][d], dp[j][d]+1)
12-
ans = max(ans, dp[i][d])
9+
for k := 0; k < i; k++ {
10+
j := nums[i] - nums[k] + 500
11+
f[i][j] = max(f[i][j], f[k][j]+1)
12+
ans = max(ans, f[i][j])
1313
}
1414
}
1515
return ans + 1

solution/1000-1099/1027.Longest Arithmetic Subsequence/Solution.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ class Solution {
22
public int longestArithSeqLength(int[] nums) {
33
int n = nums.length;
44
int ans = 0;
5-
int[][] dp = new int[n][1001];
5+
int[][] f = new int[n][1001];
66
for (int i = 1; i < n; ++i) {
7-
for (int j = 0; j < i; ++j) {
8-
int d = nums[i] - nums[j] + 500;
9-
dp[i][d] = Math.max(dp[i][d], dp[j][d] + 1);
10-
ans = Math.max(ans, dp[i][d]);
7+
for (int k = 0; k < i; ++k) {
8+
int j = nums[i] - nums[k] + 500;
9+
f[i][j] = Math.max(f[i][j], f[k][j] + 1);
10+
ans = Math.max(ans, f[i][j]);
1111
}
1212
}
1313
return ans + 1;
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution:
22
def longestArithSeqLength(self, nums: List[int]) -> int:
33
n = len(nums)
4-
dp = [[1] * 1001 for _ in range(n)]
4+
f = [[1] * 1001 for _ in range(n)]
55
ans = 0
66
for i in range(1, n):
7-
for j in range(i):
8-
d = nums[i] - nums[j] + 500
9-
dp[i][d] = max(dp[i][d], dp[j][d] + 1)
10-
ans = max(ans, dp[i][d])
7+
for k in range(i):
8+
j = nums[i] - nums[k] + 500
9+
f[i][j] = max(f[i][j], f[k][j] + 1)
10+
ans = max(ans, f[i][j])
1111
return ans

0 commit comments

Comments
 (0)