From 9972c0a61437de6cd971967e628ba4618071bb89 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 4 Dec 2024 09:57:03 +0000 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EC=8A=88=20#444=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=86=94=EB=A3=A8=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LeetCode/Best_Time_to_Buy_and_Sell_Stock.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 LeetCode/Best_Time_to_Buy_and_Sell_Stock.cpp 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