my code is giving tle on leetcode
kindly see it
o(n^2) must be accepted as per my code
Help me out in this problem
its not o(n^2) bro.
try dynamic programming…
class Solution {
public:
int longestArithSeqLength(vector<int>& A) {
int n=A.size();
vector< vector<int> > dp(n,vector<int>(1005,0));
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
int diff=A[i]-A[j]+500;
int &x=dp[i][diff],&y=dp[j][diff];
x=max(x,1+y);
ans=max(ans,1+x);
}
}
return ans;
}
};
dp state is simple.
dp[i][diff] =longest ap when array [0…i] is considered and common difference is diff
there r 4 nested loops. .
@aman212yadav in solution of faang same solution is there?
but they map<int,list> insplace of vector?
i m not sure whats there solution is but yeah map can be used in place of 2 d vector.
dp… …