Very Easy and Short C++ Solution O(1) space and O(N) time


#1
int Solution::maxProfit(const vector<int> &A) {
    if(A.size()==0||A.size()==1) return 0;
    int prediff=INT_MIN,prev_max_profit=0,max_profit=0;
    for(int i=1;i<A.size();i++)
    {
        prediff=max(prediff,-A[i-1]);
        max_profit=max(prev_max_profit,A[i]+prediff);
        prev_max_profit=max_profit;
    }
    return max_profit;
}