diff --git a/LeetCode/Best_Time_to_Buy_and_Sell_Stock.cpp b/LeetCode/Best_Time_to_Buy_and_Sell_Stock.cpp new file mode 100644 index 0000000..a3985b9 --- /dev/null +++ b/LeetCode/Best_Time_to_Buy_and_Sell_Stock.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int maxProfit(vector& prices) { + int minPrice = INT_MAX; + int maxProfit = 0; + + for (int price : prices) { + maxProfit = max(maxProfit, price - minPrice); + minPrice = min(minPrice, price); + } + + return maxProfit; + } +}; \ No newline at end of file