Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Explained solution in Clear category for Stock Profit by Selindian
def stock_profit(stock: list) -> int:
current_maximum = 0 # Initialize Variables
maximum_profit = 0 # Initialize Variables
for price in reversed(stock): # Loop over all prices in reversed order as max has to be found before min
current_maximum = max(price, current_maximum) # ... if current value is a new max, this will be our new max.
possible_profit = current_maximum - price # ... calculate a possible profit
maximum_profit = max(possible_profit, maximum_profit) # ... and get the maximum of current possible and max profit
return maximum_profit
print("Example:")
print(stock_profit([3, 1, 3, 4, 5, 1]))
assert stock_profit([2, 3, 4, 5]) == 3
assert stock_profit([3, 1, 3, 4, 5, 1]) == 4
assert stock_profit([4, 3, 2, 1]) == 0
assert stock_profit([6, 2, 1, 2, 3, 2, 3, 4, 5, 4]) == 4
assert stock_profit([1, 1, 1, 2, 1, 1, 1]) == 1
assert stock_profit([4, 3, 2, 1, 2, 1, 2, 1]) == 1
assert stock_profit([1, 1, 1, 1]) == 0
print("You are the best broker here! Click 'Check' to earn cool rewards!")
Oct. 9, 2022