121. Best Time to Buy and Sell Stock

class Solution {
public:
    int maxProfit(vector<int>& prices)
    {
        int minCost = INT_MAX, maxProfit = 0;
        for (int price : prices)
        {
            minCost = min(price, minCost);
            maxProfit = max(maxProfit, price - minCost);
        }
        return maxProfit;
    }
};
  • T: O(N)O(N)
  • S: O(1)O(1)