↓ Skip to main content
  1. Science/
  2. Computer/

0121.Best Time to Buy and Sell Stock

Solution

from typing import List
import math

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        ans, mi = 0, math.inf
        for v in prices:
            ans = max(ans, v - mi)
            mi = min(mi, v)
        return ans


# ----------------本地测试----------------
if __name__ == "__main__":
    sol = Solution()
    print(sol.maxProfit([7,1,5,3,6,4])) # 5
    print(sol.maxProfit([7,6,4,3,1]))   # 0