What's wrong with my soluyion?


#1

int Solution::maxProfit(const vector &A) {

if(A.size()==0) return 0;
int max=A[0];
int min=A[0];
int mx=0;

for(int i=1;i<A.size();i++)
{
    if(A[i]>max)
    {
        max=A[i];
        mx=(max-min);
    }
    if(A[i]<min)
    {
        min = A[i];
    }
}
return mx;

}


#2

you got the logic wrong, its not (max(array)-min(array)) that gives the right result.
suppose values were 3 9 1 8.
your final answer would be 6, whereas the answer should be 7.
You have to maximise the difference, not sell the stock at its maximum value.