# Best Time to Buy and Sell Stock
I tried to find out local maxima and local minima, and then calculate the difference between them, it turned out that this is not needed.
- Constraints/Patterns
- Keep track of the `min_price`, we always plan to sell from this point.
- Move the window forward, and calculate `profit` each time, compare with `max_profit`
```python
max_profit = 0
min_price = float('inf') # Initialize to infinity
for price in prices:
if price < min_price:
min_price = price # Update minimum price
elif price - min_price > max_profit:
max_profit = price - min_price # Update maximum profit
return max_profit
```
To make this even more efficient, also keep in track of the `max_price`, skip the verification process of those that are not the biggest.