Your question is Array Validation and Profit. 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 Grubhub pricing analysis tool receives a chronological array of integer delivery prices. Given prices and a target, return three results: whether target appears at any zero-based even index, the maximum difference between any two values, and the maximum profit from one buy followed by one later sell.
The array is valid when at least one occurrence of target is located at an even index. The maximum difference is max(prices) - min(prices), regardless of order. The maximum profit must preserve chronological order, so buying and selling on the same day is not allowed. If no profitable transaction exists, return 0.
Implement analyze_prices(prices, target). The input is a non-empty list of integers and an integer target. Return a dictionary with Boolean key valid, integer key max_difference, and integer key max_profit.
def analyze_prices(prices, target):