在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A sequence
Given a strictly increasing array (Recall that a subsequence is derived from another sequence Example 1: Input: [1,2,3,4,5,6,7,8] Output: 5 Explanation: The longest subsequence that is fibonacci-like: [1,2,3,5,8]. Example 2: Input: [1,3,7,11,12,14,18] Output: 3 Explanation: The longest subsequence that is fibonacci-like: [1,11,12], [3,11,14] or [7,11,18]. Note:
如果序列
给定一个严格递增的正整数数组形成序列,找到 (回想一下,子序列是从原序列 示例 1: 输入: [1,2,3,4,5,6,7,8] 输出: 5 解释: 最长的斐波那契式子序列为:[1,2,3,5,8] 。 示例 2: 输入: [1,3,7,11,12,14,18] 输出: 3 解释: 最长的斐波那契式子序列有: [1,11,12],[3,11,14] 以及 [7,11,18] 。 提示:
228ms 1 class Solution { 2 func lenLongestFibSubseq(_ A: [Int]) -> Int { 3 4 var dic = [Int:Int]() 5 for item in 0..<A.count{ 6 dic[A[item]] = item 7 } 8 9 var dp = [[Int]](repeating:[Int](repeating: 0, count: A.count) , count: A.count) 10 var result = 0 11 for i in 0..<A.count { 12 for j in 0..<i{ 13 if A[i] - A[j] < A[j] && dic[A[i] - A[j]] != nil { 14 dp[j][i] = dp[dic[A[i] - A[j]]!][j] + 1 15 result = max(result, dp[j][i]) 16 } 17 } 18 } 19 return result > 0 ? result + 2 : 0 20 } 21 } 236ms 1 class Solution { 2 func lenLongestFibSubseq(_ A: [Int]) -> Int { 3 var map = [Int: Int]() 4 for (index, a) in A.enumerated() { 5 map[a] = index 6 } 7 8 var result = 0 9 var dp = [[Int]](repeating: [Int](repeating: 2, count: A.count), count: A.count) 10 for i in 0 ..< A.count { 11 for j in i+1 ..< A.count { 12 let a = A[i], b = A[j], c = b - a 13 if map[c] != nil { 14 if map[c]! >= i { 15 break 16 } 17 dp[i][j] = dp[map[c]!][i] + 1 18 result = max(result, dp[i][j]) 19 } 20 } 21 } 22 23 return result 24 } 25 } 368ms 1 class Solution { 2 func lenLongestFibSubseq(_ A: [Int]) -> Int { 3 let N = A.count 4 var indexMap = [Int: Int]() 5 for i in 0..<N { 6 indexMap[A[i]] = i 7 } 8 9 var longestMap = [Int: Int]() 10 var ans = 0 11 12 for k in 0..<N { 13 for j in 0..<k { 14 guard let i = indexMap[A[k] - A[j]] else { 15 continue 16 } 17 if i < j { 18 var temp = 0 19 if let cand = longestMap[i * N + j] { 20 temp = cand + 1 21 } else { 22 temp = 3 23 } 24 longestMap[j * N + k] = temp 25 ans = max(ans, temp) 26 } 27 } 28 } 29 30 return ans >= 3 ? ans : 0 31 } 32 } 376ms 1 class Solution { 2 func lenLongestFibSubseq(_ A: [Int]) -> Int { 3 var n = A.count, res = 0 4 var dp = [[Int]](repeating: [Int](repeating: 2, count: n), count: n) 5 var m = [Int: Int]() 6 for i in 0..<n { 7 m[A[i]] = i 8 } 9 for j in 1..<n { 10 for i in 0..<j { 11 if let idx = m[A[j] - A[i]], idx < i { 12 dp[i][j] = max(dp[i][j], dp[idx][i] + 1) 13 res = max(res, dp[i][j]) 14 } 15 } 16 } 17 return res 18 } 19 } 388ms 1 class Solution { 2 func lenLongestFibSubseq(_ A: [Int]) -> Int { 3 let set = Set(A) 4 var res = 0 5 for i in 0 ..< A.count { 6 for j in i + 1 ..< A.count { 7 var a = A[i] 8 var b = A[j] 9 var c = a + b 10 var size = 2 11 while set.contains(c) { 12 size += 1 13 res = max(res, size) 14 a = b 15 b = c 16 c = a + b 17 } 18 } 19 } 20 return res 21 } 22 } 392ms 1 class Solution { 2 func lenLongestFibSubseq(_ A: [Int]) -> Int { 3 let N = A.count 4 if N <= 2 { return N } 5 6 var map = [Int: Int]() 7 for i in 0..<N { map[A[i]] = i } 8 9 var res = 0 10 var dp = Array(repeating: Array(repeating: 2, count: N), count: N) 11 for i in (0..<N - 1).reversed() { 12 for j in (i + 1..<N) { 13 if let k = map[A[i] + A[j]] { 14 dp[i][j] = dp[j][k] + 1 15 res = max(res, dp[i][j]) 16 } 17 } 18 } 19 return res 20 } 21 }
|
请发表评论