easy +10 pts

Best Time to Buy and Sell Stock

Find the maximum profit from one buy and one sell given daily prices.

Write a function `max_profit(prices)` that takes a list of non-negative integers `prices` representing the stock price on each consecutive day. The function must return the maximum profit you can achieve by choosing a single day to buy one stock and selecting a different day, strictly after the buy day, to sell that stock. If no profit is possible, return `0`. You may only complete one transaction (buy once and sell once). You must buy before you sell. Examples are provided below to clarify the expected behavior.

Constraints

- `len(prices) >= 1` - `0 <= prices[i] <= 10^5` - The time complexity of the expected solution is O(n), with O(1) extra space.

Example

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

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Track the lowest price seen so far as you iterate through the list.
At each day, compute the potential profit if you sell that day (price - min_so_far) and update the best profit.
The answer must be zero if no profitable transaction exists.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.