Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Time O(n) Space O(1) solution in Speedy category for Stock Profit by Karing
def stock_profit(stock: list[int]) -> int:
max_profit = 0
buy = stock[0]
for sell in stock:
profit = sell - buy
max_profit = max(max_profit, profit)
if sell < buy:
buy = sell
return max_profit
print("Example:")
print(stock_profit([3, 1, 3, 4, 5, 1]))
# These "asserts" are used for self-checking
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!")
April 20, 2023
Comments: