Skip to content

Commit 34ed2f4

Browse files
committed
218_Longest_Arithmetic_Subsequence_of_Given_Difference
1 parent 7fb3d66 commit 34ed2f4

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,8 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
780780
|59| **[1277. Count Square Submatrices with All Ones](https://tinyurl.com/y6a82a9r)** | [Python](https://tinyurl.com/wu6rdaw/1277_Count_Square_Submatrices_with_All_Ones.py), [Swift](https://tinyurl.com/wuja3c4/1277_Count_Square_Submatrices_with_All_Ones.swift)| [Must](https://tinyurl.com/y4sa8zgk) | Medium | --- |
781781
|60| [418. Sentence Screen Fitting](https://tinyurl.com/yhtu7lld) | [Swift](https://tinyurl.com/wuja3c4/418_Sentence_Screen_Fitting.swift)| [Art 1](https://tinyurl.com/yh4xt84c) | Medium | --- |
782782
|61| [1937. Maximum Number of Points with Cost](https://tinyurl.com/yhh6dmeh) | [Swift](https://tinyurl.com/wuja3c4/1937_Maximum_Number_of_Points_with_Cost.swift)| [Art 1](https://tinyurl.com/yg6hs4md) | Medium | --- |
783-
|61| [63. Unique Paths II](https://tinyurl.com/yjh69kzh) | [Swift](https://tinyurl.com/wuja3c4/63_Unique_Paths_II.swift)| --- | Medium | --- |
783+
|62| [63. Unique Paths II](https://tinyurl.com/yjh69kzh) | [Swift](https://tinyurl.com/wuja3c4/63_Unique_Paths_II.swift)| --- | Medium | --- |
784+
|63| [218. Longest Arithmetic Subsequence of Given Difference](https://tinyurl.com/yh8chvky) | [Swift](https://tinyurl.com/wuja3c4/218_Longest_Arithmetic_Subsequence_of_Given_Difference.swift)| [Art 1](https://tinyurl.com/yf6vygcp) | Medium | --- |
784785

785786

786787

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Brute force DFS. Wrong anserr for some cases
2+
class Solution {
3+
func longestSubsequence(_ arr: [Int], _ difference: Int) -> Int {
4+
guard arr.count > 1 else {
5+
return 1
6+
}
7+
var maxLength = 1
8+
for i in 0..<arr.count {
9+
longestSubsequenceDFSHelper(arr, difference, i, 1, &maxLength)
10+
}
11+
return maxLength
12+
}
13+
14+
func longestSubsequenceDFSHelper(_ arr: [Int], _ difference: Int, _ currentStartIndex: Int, _ currentLength: Int, _ maxLength: inout Int) {
15+
if currentStartIndex >= arr.count - 1 {
16+
maxLength = [maxLength, currentLength].max()!
17+
}
18+
for i in currentStartIndex..<arr.count {
19+
let currentDiff = arr[i] - arr[currentStartIndex]
20+
if currentDiff == difference {
21+
longestSubsequenceDFSHelper(arr, difference, i, currentLength + 1, &maxLength)
22+
}
23+
}
24+
}
25+
}
26+
27+
// DP, Accepted. Time O(n)
28+
class Solution {
29+
func longestSubsequence(_ arr: [Int], _ difference: Int) -> Int {
30+
guard arr.count > 1 else {
31+
return 1
32+
}
33+
var (maxLength, dp) = (1, [Int:Int]()) // arr itemas key and length till i as value
34+
dp[arr.first!, default: 0] += 1
35+
for i in 1..<arr.count {
36+
let pre = arr[i] - difference
37+
dp[arr[i], default: 0] = 1 + (dp[pre] ?? 0)
38+
maxLength = [maxLength, dp[arr[i]]!].max()!
39+
}
40+
return maxLength
41+
}
42+
}

0 commit comments

Comments
 (0)