,敢教日月换新天。为有牺牲多壮志

[Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10704802.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given an array A of integers, return the length of the longest arithmetic subsequence in A.

Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k]with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).

Example 1:

Input: [3,6,9,12]
Output: 4
Explanation: 
The whole array is an arithmetic sequence with steps of length = 3.

Example 2:

Input: [9,4,7,2,10]
Output: 3
Explanation: 
The longest arithmetic subsequence is [4,7,10].

Example 3:

Input: [20,1,15,3,10,5,8]
Output: 4
Explanation: 
The longest arithmetic subsequence is [20,15,10,5].

Note:

  1. 2 <= A.length <= 2000
  2. 0 <= A[i] <= 10000

给定一个整数数组 A,返回 A 中最长等差子序列的长度。

回想一下,A 的子序列是列表 A[i_1], A[i_2], ..., A[i_k] 其中 0 <= i_1 < i_2 < ... < i_k <= A.length - 1。并且如果 B[i+1] - B[i]0 <= i < B.length - 1) 的值都相同,那么序列 B 是等差的。

示例 1:

输入:[3,6,9,12]
输出:4
解释: 
整个数组是公差为 3 的等差数列。

示例 2:

输入:[9,4,7,2,10]
输出:3
解释:
最长的等差子序列是 [4,7,10]。

示例 3:

输入:[20,1,15,3,10,5,8]
输出:4
解释:
最长的等差子序列是 [20,15,10,5]。

提示:

  1. 2 <= A.length <= 2000
  2. 0 <= A[i] <= 10000

Runtime: 1424 ms
Memory Usage: 75.9 MB
复制代码
 1 class Solution {
 2     func longestArithSeqLength(_ A: [Int]) -> Int {
 3         var n:Int = A.count
 4         var map:[[Int:Int]] = [[Int:Int]](repeating:[Int:Int](),count:n)
 5         var longest:Int = 1
 6         for i in 1..<n
 7         {
 8             for j in 0..<i
 9             {
10                 var d:Int = A[i] - A[j]
11                 var l:Int = map[j][d,default:1] + 1
12                 map[i][d] = max(l, map[i][d,default:1])
13                 longest = max(longest, l)
14             }
15         }
16         return longest       
17     }
18 }
复制代码

2688ms
复制代码
 1 class Solution {
 2     func longestArithSeqLength(_ A: [Int]) -> Int {
 3         if A.count <= 2{
 4             return A.count
 5         }
 6         
 7         var dp = [Int:[Int:Int]]()
 8         var longest = 0
 9         for base in 0..<A.count{
10             let baseVal = A[base]
11             for current in base+1..<A.count{
12                 let currentVal = A[current]
13                 let diff = currentVal - baseVal
14                 
15                 dp[current, default: [Int:Int]()][diff] = max(
16                                      dp[base, default: [Int:Int]()][diff, default: 1]+1, 
17                                      dp[current, default: [Int:Int]()][diff, default: 1])
18                 
19                 longest = max(longest, dp[current]![diff]!)
20             }
21         }
22         
23         return longest
24     }
25 }
复制代码

 

posted @   为敢技术  阅读(1053)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°