File tree Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def maxProfit (self , prices : List [int ]) -> int :
3
- min_price = prices [0 ]
4
- max_price = prices [0 ]
3
+ """
4
+ maxProfit returns difference in prices[i] where maximum profit can be obtained
5
+ returns 0 if no profit can be made
6
+ prices array (1 <= prices.length <= 105), (0 <= prices[i] <= 104)
7
+ list of prices on consecutive days
8
+ """
9
+ min_price = prices [0 ] #set min_price to 1st value in prices
10
+ max_price = prices [0 ] #set max_price ot 1st value in prices
5
11
diff_price = 0
6
- for price in prices :
7
- if price < min_price :
8
- min_price = price
9
- max_price = price
12
+ for price in prices :
13
+ if price < min_price : #iterate through prices
14
+ min_price = price #assign lowest value in prices to min_price
15
+ max_price = price #assign lowest value in prices to max_price
10
16
elif price > max_price :
11
- max_price = price
17
+ max_price = price #assign highest value in prices to max_price
12
18
if diff_price < max_price - min_price :
13
- diff_price = max_price - min_price
19
+ diff_price = max_price - min_price #calculate diff_price
14
20
return diff_price
You can’t perform that action at this time.
0 commit comments