Python求股票最大收益

题目要求只能进行一次买卖,且买入的价格小于卖出的价格,否则收益为0

def maxProfit(self, prices: List[int]) -> int:
        ans=0
        for i in range(1,len(prices)):
            ans=max(ans,prices[i]-min(prices[:i]))
        return ans

我觉得我的代码很短了嘞,为啥还是超时.?有什么改进吗,可以稍微点一下方法,然后我自己去研究么

这个没看出啥问题呀。 prices 列表很大吗?