#找出所有最大值点和最小值点,作差就是所得利润 # # @param prices int整型一维数组 # @return int整型 # class Solution: def maxProfit(self, prices): # write code here buyPoint = [] salePoint = [] if len(set(prices)) == 1 or len(set(prices)) == 0: maxProfit = 0 return maxProfit if len(prices) == 2: if prices[0] < prices[1]: maxProfit = prices[1] - prices[0] return maxProfit else: if prices[0] < prices[1]: buyPoint.append(prices[0]) if prices[-1] > prices[-2]: salePoint.append(prices[-1]) for i in range(1, len(prices)-1): if prices[i - 1] < prices[i] and prices[i] >= prices[i + 1]: salePoint.append(prices[i]) elif prices[i - 1] > prices[i] and prices[i] <= prices[i + 1]: buyPoint.append(prices[i]) # print(buyPoint,end="\n") # print(salePoint) maxProfit = 0 for i in range(len(buyPoint)): maxProfit += (salePoint[i] - buyPoint[i]) return maxProfit