Thursday, August 20, 2015

Leetcode 122. Best Time to Buy and Sell Stock II

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

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 profit
Run Time: 64 ms

No comments:

Post a Comment