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.
题解:
Min
为
price[0] price[i−1]
的最小值
value[i−1]=price[i−1]−Min
返回最大值即可
import sys
MAX_=sys.maxint
class Solution(object):
def maxProfit(self, prices):
if not len(prices):
return 0
MIN=MAX_
values=[]
for i in range(len(prices)):
MIN=min(MIN,prices[i])
values.append(prices[i]-MIN)
return max(values)