Best Time to Buy and Sell Stock II

Problem: Best Time to Buy and Sell Stock II

Every time we get a price higher than previous one, we make the profit. If the previous price is a selling point, we don't sell at that time but sell at this time. If the previous one is not a selling point, we simply buy it and sell at current time.

Code in Python:

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        ans = 0
        for i in xrange(1, len(prices)):
            if prices[i] > prices[i-1]: ans += prices[i]-prices[i-1]
        return ans

results matching ""

    No results matching ""