Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

prices =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Track the minimum price seen so far and the best profit if selling today.

Approach

Scan prices once. Maintain min_price. For each price p, update best = max(best, p - min_price).

Why it works

For any sell day, the optimal buy day is the cheapest price before it; scanning maintains that invariant.

Code
Loading...
Complexity
Time: O(n)
Space: O(1)
Solution
Updated: 2026-02-23

Idea

Track the minimum price seen so far and the best profit if selling today.

Approach

Scan prices once. Maintain min_price. For each price p, update best = max(best, p - min_price).

Why it works

For any sell day, the optimal buy day is the cheapest price before it; scanning maintains that invariant.

Code
Loading...
Complexity
Time: O(n)
Space: O(1)