Easy C++ iterative approach


#1

int Solution::maxProfit(const vector &A)
{
if(A.size()<2)
{
return 0;
}
int i,val=A[0],res=INT_MIN;
vectorv(A.size());
v[0]=INT_MAX;
for(i=1;i<A.size();i++)
{
val=min(A[i],val);
v[i]=val;
res=max(res,A[i]-v[i]);
}
return res;
}