Skip to content

Commit 0a178bc

Browse files
committed
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee
1 parent 26455da commit 0a178bc

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Step 2. Add the dependency
4747

4848
https://leetcode.cn/problems/minimum-limit-of-balls-in-a-bag/
4949

50+
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee
51+
5052
https://leetcode.cn/problems/grumpy-bookstore-owner/
5153

5254
https://leetcode.cn/problems/add-edges-to-make-degrees-of-all-nodes-even/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function maxProfit(prices: number[], fee: number): number {
2+
const n = prices.length;
3+
let buy = prices[0] + fee;
4+
let profit = 0;
5+
for (let i = 1; i < n; i++) {
6+
if (prices[i] + fee < buy) {
7+
buy = prices[i] + fee;
8+
} else if (prices[i] > buy) {
9+
profit += prices[i] - buy;
10+
buy = prices[i];
11+
}
12+
}
13+
return profit;
14+
}
15+
export default maxProfit;

0 commit comments

Comments
 (0)