Skip to content

Commit c437486

Browse files
committed
Solution to 1027
1 parent 8490f0e commit c437486

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/unresolved/1027.longest-arithmetic-subsequence.jl renamed to src/problems/1027.longest-arithmetic-subsequence.jl

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1027. Longest Arithmetic Subsequence
33
# id: problem1027
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Indigo
5+
# date: 2022-04-14
66
# difficulty: Medium
77
# categories: Dynamic Programming
88
# link: <https://leetcode.com/problems/longest-arithmetic-subsequence/description/>
@@ -60,5 +60,16 @@
6060
## @lc code=start
6161
using LeetCode
6262

63-
## add your code here:
63+
function longest_arith_seq_length(nums::Vector{Int})
64+
len = length(nums)
65+
len == 1 && return 0
66+
dp = fill(0, len, 1001)
67+
res = 0
68+
for i in 1:len, j in 1:(i - 1)
69+
d = nums[i] - nums[j] + 501
70+
dp[i, d] = max(dp[i, d], dp[j, d] + 1)
71+
res = max(res, dp[i, d])
72+
end
73+
return res + 1
74+
end
6475
## @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@testset "1027.longest-arithmetic-subsequence.jl" begin
2+
@test longest_arith_seq_length([20, 1, 15, 3, 10, 5, 8]) == 4
3+
@test longest_arith_seq_length([3, 6, 9, 12]) == 4
4+
@test longest_arith_seq_length([9, 4, 7, 2, 10]) == 3
5+
@test longest_arith_seq_length([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) == 10
6+
end

0 commit comments

Comments
 (0)