Tag
Array/Dynamic Programming
Difficulty
Easy
Description
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
只能卖买一次,求最大收益
Code
import sys
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if prices==[]:
return 0
MIN = sys.maxint
Ret = 0
for i in range(len(prices)):
MIN = min(MIN,prices[i])
Ret = max(Ret,prices[i]-MIN)
return Ret