Implementations are provided below:

alt

Simple And Optimized Code:

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int n = prices.size();

        int totalProfit = 0; // this will store our maximum total profit

        // We will start looking from the second element. Why?
        // we need a element before, to compare with it
        for (int i = 1; i < n; i++) {
            // If this current price, is larger than the previous price
            // We gain a profit then
            if (prices[i] > prices[i - 1]) {
                // Add this profit to our `totalProfit`
                totalProfit += prices[i] - prices[i - 1]; // totalProfit = Selling Price - Buying Price
            }
            // else, Do nothing
        }

        return totalProfit;
    }
};
Edit

Pub: 16 Jul 2025 07:17 UTC

Views: 29