File tree 2 files changed +26
-0
lines changed
2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 326
326
705|[ Design HashSet] ( ./0705-design-hashset.js ) |Easy|
327
327
706|[ Design HashMap] ( ./0706-design-hashmap.js ) |Easy|
328
328
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|
329
330
717|[ 1-bit and 2-bit Characters] ( ./0717-1-bit-and-2-bit-characters.js ) |Easy|
330
331
720|[ Longest Word in Dictionary] ( ./0720-longest-word-in-dictionary.js ) |Medium|
331
332
722|[ Remove Comments] ( ./0722-remove-comments.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments