Leetcode-5214 Longest Arithmetic Subsequence of Given Difference(最长定差子序列)

 1 typedef long long ll;
 2 typedef pair<int,int> P;
 3 #define _for(i,a,b) for(register int i = (a);i < b;i ++)
 4 #define _rep(i,a,b) for(register int i = (a);i > b;i --)
 5 #define INF 0x3f3f3f3f
 6 #define MOD 100000000
 7 #define maxn 10003
 8 
 9 
10 class Solution
11 {
12     map<int,int> m;
13     public:
14         int longestSubsequence(vector<int>& arr, int difference)
15         {
16             int ans = 1;
17             _for(i,0,arr.size())
18             {
19                 auto iter = m.find(arr[i]-difference);
20                 if(iter==m.end())
21                     m[arr[i]] = 1;
22                 else
23                 {
24                     int k = iter->second;
25                     m[arr[i]] = k+1;
26                     ans = max(ans,k+1);
27                 }
28             }
29             return ans;
30         }
31 };

 

上一篇:[leetcode] 1.Two Sum


下一篇:C - You Are Given a WASD-string... CodeForces - 1202C