As simple as this


  • 0
    S

    Not sure why this task is Medium. Overall you just need to add every diff if previous price is lower than current. No explanation is needed. I guess the main purpose of this is to see if you can recognize the problem and how to solve it. do not overcomplicate things.

    int maxProfit(vector<int>& prices)
    {
        int totalProfit = 0;
        for (int i=1; i<prices.size(); ++i)
        {
            if (prices[i] > prices[i-1])
                totalProfit += (prices[i] - prices[i-1]);
        }
        
        return totalProfit;
    }

  • 0

    I solved it with dynamic programming, and then I saw your solution.
    Maybe I think too much.


Log in to reply
 

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