class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices) == 0 or len(prices) == 1:
return 0
minP = prices[0]
result = 0
for price in prices:
# Maintain the maximum profit at each price
result = max(result, price - minP)
# Maintain the minimum price seen so far
minP = min(minP, price)
if result < 0:
return 0
return result
Written with StackEdit.