Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 662 Bytes

best-time-to-buy-and-sell-stock.MD

File metadata and controls

30 lines (23 loc) · 662 Bytes

Best Time to Buy and Sell Stock @ LeetCode

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/


Observations

  • Need to keep min_price before current. max_price could be taken immediately.

1st Approach

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int min_price = INT_MAX;
        int max_profit = 0;
        
        for (auto &price: prices) {
            if (price < min_price) {
                min_price = price;
            } else if (price - min_price > max_profit) {
                max_profit = price - min_price;
            }
        }
        
        return max_profit;
    }
};