C++ Short and Simple, O(N) Time O(1) Space


  • 0
    M
    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int maxp = 0;
            for (int i = 1; i < prices.size(); ++i)
                maxp += max(0, prices[i] - prices[i - 1]);
            return maxp;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.