Skip to content

Commit 33af296

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Update 714. Best Time to Buy and Sell Stock with Transaction Fee.py
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent ce17f91 commit 33af296

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int], fee: int) -> int:
3+
@cache
4+
def dfs(i: int, j: int) -> int:
5+
if i >= len(prices):
6+
return 0
7+
ans = dfs(i + 1, j)
8+
if j:
9+
ans = max(ans, prices[i] + dfs(i + 1, 0) - fee)
10+
else:
11+
ans = max(ans, -prices[i] + dfs(i + 1, 1))
12+
return ans
13+
14+
return dfs(0, 0)

0 commit comments

Comments
 (0)