Your question is Max Profit From Prices. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
A monitoring component in Shift Technology's Force platform receives a sequence of daily values. Given an array of stock prices in chronological order, determine the maximum profit possible from one transaction: buy on exactly one day and sell on a later day. If no profitable transaction exists, return 0.
You must buy before selling, and each day can be used at most once. Return only the maximum profit, not the buy and sell indices.
Implement max_profit(prices), where prices is a list of integers and prices[i] is the stock price on day i. Return an integer representing the greatest value of prices[j] - prices[i] for all pairs where i < j. Return 0 when every valid transaction loses money or when fewer than two prices are provided.
def max_profit(prices):