Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 416c5d9

Browse files
committed
solve problem Best Time To Buy And Sell Stock With Transaction Fee
1 parent 74a7baa commit 416c5d9

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ All solutions will be accepted!
9797
|696|[Count Binary Substrings](https://leetcode-cn.com/problems/count-binary-substrings/description/)|[java/py/js](./algorithms/CountBinarySubstrings)|Easy|
9898
|121|[Best Time To Buy And Sell Stock](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/)|[java/py/js](./algorithms/BestTimeToBuyAndSellStock)|Easy|
9999
|122|[Best Time To Buy And Sell Stock II](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/)|[java/py/js](./algorithms/BestTimeToBuyAndSellStockII)|Easy|
100+
|714|[Best Time To Buy And Sell Stock With Transaction Fee](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)|[java/py/js](./algorithms/BestTimeToBuyAndSellStockWithTransactionFee)|Medium|
100101
|599|[Minimum Index Sum Of Two Lists](https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/description/)|[java/py/js](./algorithms/MinimumIndexSumOfTwoLists)|Easy|
101102
|551|[Student Attendance Record I](https://leetcode-cn.com/problems/student-attendance-record-i/description/)|[java/py/js](./algorithms/StudentAttendanceRecordI)|Easy|
102103
|538|[Convert Bst To Greater Tree](https://leetcode-cn.com/problems/convert-bst-to-greater-tree/description/)|[java/py/js](./algorithms/ConvertBstToGreaterTree)|Easy|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Best Time To Buy And Sell Stock With Transaction Fee
2+
We can solve this problem by Greedy Algorithm
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int maxProfit(int[] prices, int fee) {
3+
if (prices.length == 0)
4+
return 0;
5+
6+
int maxProfit = 0,
7+
holder = prices[0],
8+
maxPrice = prices[0];
9+
10+
for (int i = 1; i < prices.length; i++) {
11+
int price = prices[i];
12+
13+
if (price > maxPrice)
14+
maxPrice = price;
15+
else if (maxPrice - price >= fee) {
16+
if (maxPrice - holder > fee)
17+
maxProfit += maxPrice - holder - fee;
18+
holder = price;
19+
maxPrice = price;
20+
} else if (price < holder) {
21+
holder = price;
22+
maxPrice = price;
23+
}
24+
}
25+
26+
if (maxPrice - holder > fee)
27+
maxProfit += maxPrice - holder - fee;
28+
return maxProfit;
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} prices
3+
* @param {number} fee
4+
* @return {number}
5+
*/
6+
var maxProfit = function(prices, fee) {
7+
if (prices.length == 0)
8+
return 0
9+
10+
let maxProfit = 0,
11+
holder = prices[0],
12+
maxPrice = prices[0]
13+
14+
for (let i = 1; i < prices.length; i++) {
15+
let price = prices[i]
16+
17+
if (price > maxPrice)
18+
maxPrice = price
19+
else if (maxPrice - price >= fee) {
20+
if (maxPrice - holder > fee)
21+
maxProfit += maxPrice - holder - fee
22+
holder = price
23+
maxPrice = price
24+
} else if (price < holder) {
25+
holder = price
26+
maxPrice = price
27+
}
28+
}
29+
30+
if (maxPrice - holder > fee)
31+
maxProfit += maxPrice - holder - fee
32+
return maxProfit
33+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def maxProfit(self, prices, fee):
3+
"""
4+
:type prices: List[int]
5+
:type fee: int
6+
:rtype: int
7+
"""
8+
if len(prices) == 0:
9+
return 0
10+
11+
holder = prices[0]
12+
max_price = prices[0]
13+
max_profit = 0
14+
15+
for i in xrange(1, len(prices)):
16+
price = prices[i]
17+
if price > max_price:
18+
max_price = price
19+
elif max_price - price >= fee:
20+
if max_price - holder - fee > 0:
21+
max_profit += max_price - holder - fee
22+
holder = price
23+
max_price = price
24+
elif price < holder:
25+
holder = price
26+
max_price = price
27+
28+
if max_price - holder > fee:
29+
max_profit += max_price - holder - fee
30+
return max_profit
31+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /