classSolution(object): defmaxProfitPartial(self, prices, start, end): n = end - start + 1 if n <= 1: return0 profit = 0 cur_min = prices[start] for i in xrange(start + 1, end + 1): profit = max(profit, prices[i] - cur_min) cur_min = min(cur_min, prices[i]) return profit
defmaxProfit(self, prices): """ :type prices: List[int] :rtype: int """ max_profit = 0 n = len(prices) if n <= 1: return0 for i in xrange(n): profit = self.maxProfitPartial(prices, 0, i) + self.maxProfitPartial(prices, i, n - 1) max_profit = max(max_profit, profit) return max_profit
classSolution(object): defmaxProfitPartial(self, prices, start, end): n = end - start + 1 if n <= 1: return0 profit = 0 cur_min = prices[start] for i in xrange(start + 1, end + 1): profit = max(profit, prices[i] - cur_min) cur_min = min(cur_min, prices[i]) return profit
defmaxProfit(self, prices): """ :type prices: List[int] :rtype: int """ n = len(prices) if n <= 1: return0 left = [0] * n cur_min = prices[0] for i in xrange(1, n): left[i] = max(left[i - 1], prices[i] - cur_min) cur_min = min(cur_min, prices[i])