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
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 3f095f2

Browse files
created solution for problem65
1 parent 1a7e67f commit 3f095f2

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎problem65/Solution.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int], fee: int) -> int:
3+
days = len(prices)
4+
has_buy_cache = [False] * days
5+
buy_cache = [None] * days
6+
7+
def buy(day):
8+
if day == days: return 0
9+
if has_buy_cache[day]: return buy_cache[day]
10+
11+
# Buy stock this day & sell another day
12+
profit = sell(day+1) - prices[day]
13+
14+
# Buy stock another day
15+
profit = max(profit, buy(day+1))
16+
17+
buy_cache[day] = profit
18+
has_buy_cache[day] = True
19+
return profit
20+
21+
has_sell_cache = [False] * days
22+
sell_cache = [None] * days
23+
24+
def sell(day):
25+
if day == days: return 0
26+
if has_sell_cache[day]: return sell_cache[day]
27+
28+
# Sell stock this day & buy another day
29+
profit = prices[day] + buy(day+1) - fee
30+
31+
# Sell stock another day
32+
profit = max(profit, sell(day+1))
33+
34+
sell_cache[day] = profit
35+
has_sell_cache[day] = True
36+
return profit
37+
38+
return buy(0)

0 commit comments

Comments
(0)

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