Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

Best Time to Buy and Sell Stock 🌟

ONLY ONE TRANSACTION IS ALLOWED

Solution

Code

int maximumProfit(vector<int>& prices)
{
    int mini = prices[0];
    int maxProfit = 0;
    int n = prices.size();
    for (int i = 1; i < n; i++) {
        int cost = prices[i] - mini;
        maxProfit = max(maxProfit, cost);
        mini = min(mini, prices[i]);
    }
    return maxProfit;
}

Other