Skip to content

Commit d87edd8

Browse files
committed
1218 added.
1 parent 091c363 commit d87edd8

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(B)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(B main2.cpp)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/// Source : https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-10-05
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
9+
using namespace std;
10+
11+
12+
/// Dynamic Programming
13+
/// Time Complexity: O(n)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
int longestSubsequence(vector<int>& arr, int difference) {
18+
19+
map<int, int> pos;
20+
vector<int> dp(arr.size(), 1);
21+
for(int i = 0; i < arr.size(); i ++){
22+
int pre = arr[i] - difference;
23+
if(pos.count(pre))
24+
dp[i] = max(dp[i], dp[pos[pre]] + 1);
25+
pos[arr[i]] = i;
26+
}
27+
return *max_element(dp.begin(), dp.end());
28+
}
29+
};
30+
31+
32+
int main() {
33+
34+
vector<int> arr1 = {1,2,3,4};
35+
cout << Solution().longestSubsequence(arr1, 1) << endl;
36+
// 4
37+
38+
vector<int> arr2 = {1,3,5,7};
39+
cout << Solution().longestSubsequence(arr2, 1) << endl;
40+
// 1
41+
42+
vector<int> arr3 = {1,5,7,8,5,3,4,2,1};
43+
cout << Solution().longestSubsequence(arr3, -2) << endl;
44+
// 4
45+
46+
return 0;
47+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/// Source : https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-10-06
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
9+
using namespace std;
10+
11+
12+
/// Dynamic Programming
13+
/// More Concise Logic
14+
/// Time Complexity: O(n)
15+
/// Space Complexity: O(n)
16+
class Solution {
17+
public:
18+
int longestSubsequence(vector<int>& arr, int difference) {
19+
20+
map<int, int> dp;
21+
int res = 1;
22+
for(int i = 0; i < arr.size(); i ++){
23+
dp[arr[i]] = max(dp[arr[i]], dp[arr[i] - difference] + 1);
24+
res = max(res, dp[arr[i]]);
25+
}
26+
return res;
27+
}
28+
};
29+
30+
31+
int main() {
32+
33+
vector<int> arr1 = {1,2,3,4};
34+
cout << Solution().longestSubsequence(arr1, 1) << endl;
35+
// 4
36+
37+
vector<int> arr2 = {1,3,5,7};
38+
cout << Solution().longestSubsequence(arr2, 1) << endl;
39+
// 1
40+
41+
vector<int> arr3 = {1,5,7,8,5,3,4,2,1};
42+
cout << Solution().longestSubsequence(arr3, -2) << endl;
43+
// 4
44+
45+
return 0;
46+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,4 +863,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
863863
| 1210 | [Minimum Moves to Reach Target with Rotations](https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations/) | [] | [C++](1210-Minimum-Moves-to-Reach-Target-with-Rotations/cpp-1210/) | | |
864864
| | | | | | |
865865
| 1217 | [Play with Chips](https://leetcode.com/problems/play-with-chips/) | [] | [C++](1217-Play with Chips/cpp-1217/) | | |
866+
| 1218 | [Longest Arithmetic Subsequence of Given Difference](https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/) | [] | [C++](1218-Longest-Arithmetic-Subsequence-of-Given-Difference/cpp-1218/) | | |
866867
| | | | | | |

0 commit comments

Comments
 (0)