Skip to content

Commit 443044a

Browse files
authored
Create 714. Best Time to Buy and Sell Stock with Transaction Fee
1 parent c226723 commit 443044a

File tree

1 file changed

+17
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)