From bb796ab51f7496a6adfd8074bedd47848f540ee0 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 4 Aug 2024 23:25:17 +0200 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB714.=20Best=20Time=20to=20Buy?= =?UTF-8?q?=20and=20Sell=20Stock=20with=20Transaction=20Fee=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...me-to-buy-and-sell-stock-with-transaction-fee.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 cs/best-time-to-buy-and-sell-stock-with-transaction-fee.cs diff --git a/cs/best-time-to-buy-and-sell-stock-with-transaction-fee.cs b/cs/best-time-to-buy-and-sell-stock-with-transaction-fee.cs new file mode 100644 index 0000000..70dd792 --- /dev/null +++ b/cs/best-time-to-buy-and-sell-stock-with-transaction-fee.cs @@ -0,0 +1,13 @@ +public class Solution { + public int MaxProfit(int[] prices, int fee) { + var profit = 0; + var buyPrice = prices[0]; + + for (var i = 1; i < prices.Length; ++i) { + profit = Math.Max(profit, prices[i] - buyPrice - fee); + buyPrice = Math.Min(buyPrice, prices[i] - profit); + } + + return profit; + } +}