mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cs: add «714. Best Time to Buy and Sell Stock with Transaction Fee»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
1ae0cb4b89
commit
bb796ab51f
1 changed files with 13 additions and 0 deletions
13
cs/best-time-to-buy-and-sell-stock-with-transaction-fee.cs
Normal file
13
cs/best-time-to-buy-and-sell-stock-with-transaction-fee.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue