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; + } +}