Simplest Efficient Solution


#1

int Solution::maxProfit(const vector &A) {
int profit=0;
int cp=INT_MAX; //cp stands for cost price
if(A.size()==0||A.size()==1)
return 0;
for(int i=0;i<A.size();i++)
{
cp=min(A[i],cp); //if the new stock is less then buy else skip
profit=max(profit,A[i]-cp); //profit is calculated by subtracting the cp from current price(selling price)
}
return profit;
}