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.
求进行一次股票交易所能得到的最大收益~
class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
if prices is None or len(prices) == 0: return 0
minVal, maxProfit = prices[0], 0
for i in xrange(1, len(prices)):
maxProfit = max(maxProfit, prices[i] - minVal)
minVal = min(minVal, prices[i])
return maxProfit