4
4
# comments: This program output the
5
5
# best time to buy and sell stock with fees
6
6
#############################
7
- import doctest
7
+ from typing import List
8
8
9
9
10
10
class Solution :
11
- def solve (self , prices , index , fee , buy , dp ):
11
+ def solve (
12
+ self , prices : List [int ], index : int , fee : int , buy : int , dp : List [List [int ]]
13
+ ) -> int :
14
+ """
15
+ Calculate the maximum profit with the given parameters.
16
+
17
+ :param prices: List of prices for each day.
18
+ :param index: Current day index.
19
+ :param fee: Transaction fee.
20
+ :param buy: Buy or sell flag (1 for buy, 0 for sell).
21
+ :param dp: Memoization table.
22
+ :return: Maximum profit achievable.
23
+ """
12
24
if index >= len (prices ):
13
25
return 0
14
26
if dp [index ][buy ] != - 1 :
@@ -29,15 +41,25 @@ def solve(self, prices, index, fee, buy, dp):
29
41
dp [index ][buy ] = profit
30
42
return profit
31
43
32
- def maxprofit (self , prices , fee ):
44
+ def maxprofit (self , prices : List [int ], fee : int ) -> int :
45
+ """
46
+ Calculate the maximum profit achievable when buying and
47
+ selling stocks with a fee.
48
+
49
+ :param prices: List of stock prices for each day.
50
+ :param fee: Transaction fee.
51
+ :return: Maximum profit achievable.
52
+ """
33
53
n = len (prices )
34
54
dp = [[- 1 ] * 2 for _ in range (n )]
35
55
36
56
return self .solve (prices , 0 , fee , 1 , dp )
37
57
38
58
39
59
def main ():
40
- ## This is the function for taking input and output ##
60
+ """
61
+ This is the function for taking input and output.
62
+ """
41
63
prices = list (map (int , input ("Enter the list of prices: " ).split ()))
42
64
fee = int (input ("Enter the transaction fee: " ))
43
65
@@ -48,5 +70,7 @@ def main():
48
70
49
71
50
72
if __name__ == "__main__" :
73
+ import doctest
74
+
51
75
doctest .testmod ()
52
76
main ()
0 commit comments