From 8f9fde289bc4f7c2948c9a6dcbb397eff8f31711 Mon Sep 17 00:00:00 2001 From: colorbox Date: Mon, 18 Mar 2024 00:22:19 +0900 Subject: [PATCH 1/2] Add solved codes for `121. Best Time to Buy and Sell Stock` --- 121/1.cpp | 26 ++++++++++++++++++++++++++ 121/2.cpp | 18 ++++++++++++++++++ 121/3.cpp | 16 ++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 121/1.cpp create mode 100644 121/2.cpp create mode 100644 121/3.cpp diff --git a/121/1.cpp b/121/1.cpp new file mode 100644 index 0000000..05d1a7f --- /dev/null +++ b/121/1.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int maxProfit(vector& prices) { + vector minPricesFromStart; + vector maxPricesFromEnd; + + int minPrice = INT_MAX; + for(int i = 0; i < prices.size(); i++){ + minPrice = min(minPrice, prices[i]); + minPricesFromStart.push_back(minPrice); + } + int maxPrice = -1; + for(int i = prices.size()-1; i >= 0; i--){ + maxPrice = max(maxPrice, prices[i]); + maxPricesFromEnd.push_back(maxPrice); + } + reverse(maxPricesFromEnd.begin(), maxPricesFromEnd.end()); + + int maxProfit = 0; + for(int i = 0; i < prices.size(); i++){ + int currentProfit = maxPricesFromEnd[i] - minPricesFromStart[i]; + maxProfit = max(maxProfit, currentProfit); + } + return maxProfit; + } +}; diff --git a/121/2.cpp b/121/2.cpp new file mode 100644 index 0000000..79b34ef --- /dev/null +++ b/121/2.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int maxProfit(vector& prices) { + int maxPrice = 0; + int minPrice = INT_MAX; + int maxProfit = 0; + for (int i = 0; i < prices.size(); i++) { + if (prices[i] < minPrice) { + minPrice = prices[i]; + } else { + maxPrice = prices[i]; + int currentProfit = maxPrice - minPrice; + maxProfit = max(maxProfit, currentProfit); + } + } + return maxProfit; + } +}; diff --git a/121/3.cpp b/121/3.cpp new file mode 100644 index 0000000..e2e117e --- /dev/null +++ b/121/3.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int maxProfit(vector& prices) { + int minPrice = INT_MAX; + int maxProfit = 0; + for(int i = 0; i < prices.size(); i++) { + if (prices[i] < minPrice) { + minPrice = prices[i]; + } else { + maxProfit = max(maxProfit, prices[i] - minPrice); + } + } + + return maxProfit; + } +}; From 72d4df2d8f6665dbafdf885d8b5522c2806931ae Mon Sep 17 00:00:00 2001 From: colorbox Date: Tue, 19 Mar 2024 01:44:14 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E6=8C=87=E6=91=98=E3=82=92=E6=94=B9=E5=96=84=E3=81=97=E3=81=9F?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 121/4.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 121/4.cpp diff --git a/121/4.cpp b/121/4.cpp new file mode 100644 index 0000000..885bf82 --- /dev/null +++ b/121/4.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int maxProfit(vector& prices) { + if(prices.empty())abort(); + + int minPrice = prices[0]; + int currentProfit = 0; + for(int price: prices) { + if (price < minPrice) { + minPrice = price; + } else { + currentProfit = max(currentProfit, price - minPrice); + } + } + + return currentProfit; + } +};