← Programming questions
Medium / PYTHON

Maximum Profit Trades

arrays · greedy 20 minute study target

Not completed

Challenge

Implement maximum_profit(prices) for a list of non-negative integer daily prices. You may complete any number of buy-then-sell transactions, but hold at most one share at a time. You begin and end with no shares. There are no fees, short sales, or cooldowns; selling and buying again on the same day is allowed. Return the maximum total integer profit. Fewer than two prices returns 0. Aim for O(n).

Target complexity: O(n)

Return values are checked by type. Use the exact function name. Print output is not your answer. Tests have time and memory limits.

Public examples

Example 1

Arguments

[[2, 5, 3, 8]]

Expected return

8
Example 2

Arguments

[[7, 6, 4, 3, 1]]

Expected return

0
Hint

Which positive adjacent changes could you capture?

The worked solution unlocks after completion, or with Premium access.

maximum_profit.pyPython 3.12

Drafts are saved to your account, not to shared browser storage.

Sign in to submit code and save progress.

Results

Your results will appear here.

Hidden tests reveal only their pass/fail status, never their inputs or expected outputs. Runtime is observed wall time—not an inferred complexity class.

© 2026 EverythingQuant. All Rights Reserved.