Skip to content

Commit 37fbe1c

Browse files
authored
Create 1027. Longest Arithmetic Subsequence.cpp
1 parent 951fede commit 37fbe1c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int dp[1001][1001];
4+
int longestArithSeqLength(vector<int>& nums) {
5+
memset(dp , 0 , sizeof(dp));
6+
int n=nums.size();
7+
int maxlen =1;
8+
for(int i=1 ; i<n ;i++)
9+
{
10+
for(int j=0;j<i;j++)
11+
{
12+
int diff=nums[i]-nums[j] + 500;
13+
14+
dp[i][diff] = max(dp[i][diff] , 1 + dp[j][diff]);
15+
16+
maxlen = max(maxlen , dp[i][diff]);
17+
18+
}
19+
}
20+
21+
return maxlen + 1;
22+
}
23+
24+
};

0 commit comments

Comments
 (0)