Skip to content

Commit 6e83c7e

Browse files
committed
Add solution #714
1 parent 9e2c366 commit 6e83c7e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@
326326
705|[Design HashSet](./0705-design-hashset.js)|Easy|
327327
706|[Design HashMap](./0706-design-hashmap.js)|Easy|
328328
713|[Subarray Product Less Than K](./0713-subarray-product-less-than-k.js)|Medium|
329+
714|[Best Time to Buy and Sell Stock with Transaction Fee](./0714-best-time-to-buy-and-sell-stock-with-transaction-fee.js)|Medium|
329330
717|[1-bit and 2-bit Characters](./0717-1-bit-and-2-bit-characters.js)|Easy|
330331
720|[Longest Word in Dictionary](./0720-longest-word-in-dictionary.js)|Medium|
331332
722|[Remove Comments](./0722-remove-comments.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 714. Best Time to Buy and Sell Stock with Transaction Fee
3+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array prices where prices[i] is the price of a given stock on the ith day,
7+
* and an integer fee representing a transaction fee.
8+
*
9+
* Find the maximum profit you can achieve. You may complete as many transactions as you like,
10+
* but you need to pay the transaction fee for each transaction.
11+
*/
12+
13+
/**
14+
* @param {number[]} prices
15+
* @param {number} fee
16+
* @return {number}
17+
*/
18+
var maxProfit = function(prices, fee) {
19+
let result = 0;
20+
for (let i = 1, sell = -prices[0]; i < prices.length; i++) {
21+
result = Math.max(result, sell + prices[i] - fee);
22+
sell = Math.max(sell, result - prices[i]);
23+
}
24+
return result;
25+
};

0 commit comments

Comments
 (0)