#32EasySliding Window
Best Time to Buy and Sell Stock
Given daily stock prices, choose one day to buy and a later day to sell for the maximum possible profit. Return 0 if no profitable sale exists.
Constraints
- •
1 ≤ prices.length ≤ 10⁵ - •
0 ≤ prices[i] ≤ 10⁴
Solution inPython
Python
def maxProfit(prices: list[int]) -> int:
low, profit = float('inf'), 0
for price in prices:
low = min(low, price)
profit = max(profit, price - low)
return profit