Skip to content

Commit 049b0fc

Browse files
authored
Create best-time-to-buy-and-sell-stock-with-transaction-fee.cpp
1 parent e4d68c6 commit 049b0fc

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int maxProfit(vector<int>& prices, int fee) {
7+
int cash = 0, hold = -prices[0];
8+
for (int i = 1; i < prices.size(); ++i) {
9+
cash = max(cash, hold + prices[i] - fee);
10+
hold = max(hold, cash - prices[i]);
11+
}
12+
return cash;
13+
}
14+
};

0 commit comments

Comments
 (0)