Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
还是求股票交易的最大利润,因为交易次数是不限定得,所以只要对于每次两天差价大于0的都进行交易,就可以得到最大利润~代码如下
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
maxProfit = 0
for i in xrange(1, len(prices)):
if prices[i] - prices[i - 1] > 0:
maxProfit += prices[i] - prices[i - 1]
return maxProfit