您得到一个数组价格,价格[i]是给定股票的价格在第一个股票购买日.您希望通过选择一个单一的股票购买日和选择未来的其他日来销售该股票来最大化利.返回您可以从此交易中获得的最大利.如果您无法获得任何利,返回0.
To solve this problem, you can keep track of the minimum price seen so far and the maximum profit that can be made by selling at the current price. You iterate through the array and update the minimum price and maximum profit as you go along. Finally, return the maximum profit.

Here's the code:

```
def maxProfit(prices):
    if not prices:
        return 0
    
    min_price = prices[0]
    max_profit = 0
    
    for price in prices:
        if price < min_price:
            min_price = price
        else:
            profit = price - min_price
            if profit > max_profit:
                max_profit = profit
                
    return max_profit
```

Example usage:
```
print(maxProfit([7,1,5,3,6,4])) # Expected output: 5 (buy on day 2 at price 1 and sell on day 5 at price 6)
print(maxProfit([7,6,4,3,1])) # Expected output: 0 (cannot make a profit)
```