24. Best time to buy and sell

EasyProgramFree10 points

prices lists a stock’s price on consecutive days. You may buy once and sell once, on a later day. Write maxProfit prices, the largest profit you can make, or 0 if no sale makes a profit.

The hidden tests include 20,000 days of prices, so comparing every pair of days will run out of time.

Examples

  1. InputmaxProfit [7, 1, 5, 3, 6, 4]Output5
  2. InputmaxProfit [7, 6, 4, 3, 1]Output0

Submitting also runs 7 hidden tests.

Solution.lean
Loading editor…

Run checks your code against the examples and your custom inputs and records nothing. Submit also runs the hidden tests, confirms the axioms your proof uses and records the result. Each uses one compiler check.

ReadyLn 1, Col 1Lean 4
Draft saved in this browser