Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit a55944b

Browse files
committed
created another solution for problem65
1 parent 3f095f2 commit a55944b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
File renamed without changes.

problem65/Solution1.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int], fee: int) -> int:
3+
# For memoization
4+
cache = [[-1] * 2 for _ in range(len(prices))]
5+
6+
def findProfit(i, canBuy):
7+
if i == len(prices): return 0
8+
if cache[i][canBuy] != -1: return cache[i][canBuy]
9+
profit = 0
10+
11+
if canBuy:
12+
# Choose max between buy & dont buy today
13+
profit = max(
14+
-prices[i] + findProfit(i+1, 0),
15+
findProfit(i+1, 1)
16+
)
17+
else:
18+
# Choose max between sell & sell another day
19+
profit = max(
20+
prices[i] + findProfit(i+1, 1) - fee,
21+
findProfit(i+1, 0)
22+
)
23+
24+
# Memoization
25+
cache[i][canBuy] = profit
26+
return profit
27+
28+
return findProfit(0, 1)

0 commit comments

Comments
 (0)