Largest peak-to-trough drawdown
Asked at Citadel
You are given a price path as a list. The maximum drawdown is the largest drop from a prior peak down to a later point: the biggest value of price[i] - price[j] where j comes after i. Report it as a nonnegative number, or 0 if prices never fall.
prices = [100, 90, 120, 80, 130]
-> 40 # peak 120 falls to 80
prices = [1, 2, 3, 4]
-> 0 # only rises; no drawdown
Return the maximum drawdown. Aim for time and space.
Your answer
This one is open-ended. Work it through, then check your reasoning against the full solution.