Skip to content

Commit 2b57f0f

Browse files
author
Partho Biswas
committed
714. Best Time to Buy and Sell Stock with Transaction Fee
1 parent 1f47b39 commit 2b57f0f

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,11 @@ I have solved quite a number of problems from several topics. See the below tabl
304304
|13| [5. Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)| [Python](leetcode.com/python/5_Longest_Palindromic_Substring.py)| --- | Medium | 📌 Classic DP |
305305
|14| [1066. Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/)| [Python](leetcode.com/python/1066_Campus_Bikes_II.py)| | Medium |📌 TODO: Backtracking solution is getting TLE. Solve it and implement it with DP also |
306306
|15| [121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [Python](leetcode.com/python/121_Best_Time_to_Buy_and_Sell_Stock.py)| [Vid 1](https://codinginterviewclass.com/courses/coding-interview-class/lectures/12305111) | Easy | Fundamental |
307-
|16| [122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| [Python](leetcode.com/python/122_Best_Time_to_Buy_and_Sell_Stock_II.py)| [Video 01](https://www.youtube.com/watch?v=blUwDD6JYaE)| Easy | Fundamentals |
307+
|16| [122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| [Python](leetcode.com/python/122_Best_Time_to_Buy_and_Sell_Stock_II.py)| [Video 01](https://www.youtube.com/watch?v=blUwDD6JYaE)| Easy | More like Greedy |
308308
|17| [123. Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| [Python](leetcode.com/python/123_Best_Time_to_Buy_and_Sell_Stock_III.py)| **[Vid 1](https://www.youtube.com/watch?v=oDhu5uGq_ic&t=2s), [Vid 2](https://www.youtube.com/watch?v=Pw6lrYANjz4)** | Hard | Fundamental |
309-
|18| [188. Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| [Python](leetcode.com/python/188_Best_Time_to_Buy_and_Sell_Stock_IV.py)| **[Vid 1](https://www.youtube.com/watch?v=oDhu5uGq_ic&t=2s), [Vid 2](https://www.youtube.com/watch?v=Pw6lrYANjz4)** | Hard | Fundamentals |
310-
|19| [309. Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)| [Python](leetcode.com/python/309_Best_Time_to_Buy_and_Sell_Stock_with_Cooldown.py)| --- | Medium | Fundamental |
311-
|20| [714. Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)| [Python](leetcode.com/python/121_Best_Time_to_Buy_and_Sell_Stock.py)| --- | Medium | Fundamental |
309+
|18| [188. Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| [Python](leetcode.com/python/188_Best_Time_to_Buy_and_Sell_Stock_IV.py)| **[Vid 1](https://www.youtube.com/watch?v=oDhu5uGq_ic&t=2s), [Vid 2](https://www.youtube.com/watch?v=Pw6lrYANjz4)** | Hard | **Getting "Memory Limit Exceeded"** |
310+
|19| [309. Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)| [Python](leetcode.com/python/309_Best_Time_to_Buy_and_Sell_Stock_with_Cooldown.py)| **[Must Read](https://tinyurl.com/y7eaa7vk) | Medium | Fundamental |
311+
|20| [714. Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)| [Python](leetcode.com/python/714_Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee.py)| **[Must Read](https://tinyurl.com/y7eaa7vk)** | Medium | More like Greedy, but DP |
312312

313313

314314
### Bit Manipulation
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
days = len(prices)
9+
totalProfit = 0
10+
minimumPrice = prices[0]
11+
for today in range(1, days):
12+
if prices[today] < minimumPrice:
13+
minimumPrice = prices[today]
14+
elif minimumPrice < (prices[today] - fee):
15+
totalProfit += (prices[today] - minimumPrice - fee)
16+
minimumPrice = prices[today] - fee # https://tinyurl.com/unnnjwx and https://tinyurl.com/umjax4y
17+
return totalProfit
18+
19+
20+
# def maxProfit(self, prices, fee):
21+
# """
22+
# :type prices: List[int]
23+
# :type fee: int
24+
# :rtype: int
25+
# """
26+
# days = len(prices)
27+
# if days < 2:
28+
# return 0
29+
# profit = 0
30+
# minimumPrice = prices[0]
31+
# for i in range(1, days):
32+
# if prices[i] < minimumPrice:
33+
# minimumPrice = prices[i]
34+
# elif prices[i] > minimumPrice + fee:
35+
# profit += prices[i] - fee - minimumPrice
36+
# minimumPrice = prices[i] - fee
37+
# return profit
38+
39+
40+
41+
42+
sol = Solution()
43+
prices = [1,3,7,5,10,3]
44+
fee = 3
45+
output = sol.maxProfit(prices, fee)
46+
print('Res: ', output)

0 commit comments

Comments
 (0)