Still easy. On each step, if we can make money, we take it. All those profits will sum to result.
Solution:
# T:O(n) S:O(1) class Solution: # @param prices, a list of integer # @return an integer def maxProfit(self, prices): profit = 0 for i in xrange(len(prices) - 1): profit += max(0, prices[i + 1] - prices[i]) return profitRun Time: 64 ms
No comments:
Post a Comment