easy +10 pts

Stock Buy Sell Once

Find the maximum profit from one buy and one sell of a stock over time.

You are given a list `prices` where `prices[i]` is the price of a given stock on day `i` (0-indexed). You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Write a function `max_profit(prices)` that returns the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return `0`. - You must buy before you sell (the sell day must be after the buy day). - You may only complete one transaction. - The input list may be empty; in that case return `0`.

Constraints

`0 <= len(prices) <= 10^5` `0 <= prices[i] <= 10^5` Time complexity expected: O(n). Space complexity: O(1).

Example

```python
>>> max_profit([7,1,5,3,6,4])
5
>>> max_profit([7,6,4,3,1])
0
>>> max_profit([])
0
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Track the minimum price seen so far.
For each price, consider selling at that price to get a candidate profit.
Keep the maximum candidate profit as you iterate.
If all prices are decreasing, the max profit remains zero.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.