FunctionRacerProblemsRace

Largest Drawdown

EasyArraysPython, JavaScript, TypeScript, Java, C++

prices lists a fund's price on consecutive days. The drawdown on day j is the highest price seen on any day up to and including day j, minus the price on day j.

Return the largest drawdown over all days. It is never negative: when no price ever falls below an earlier high, or prices is empty, return 0.

Example 1

prices [10,7,12,4,9]
returns 8

The high is 10, then 12 from day 2 on. On day 3 the price is 4, which is 8 below the high of 12.

Example 2

prices [1,2,3]
returns 0

Prices only rise, so every day is at its own high.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems