122 Best Time to Buy and Sell Stock II

Published:

Problem

Solution

Intuition

The problem requires us to find the maximum possible profit available with multiple buying and selling of stock. We are allowed to buy and sell on the same day so we just need to add up all cases where the stock goes up on the following day.

Approach

We check if the price of the stock is greater than it was in the previous day and add it to the total profit if it is. If the stock goes down, we don’t add it to the current profit.

Complexity

  • Time complexity: $O(n)$ as there is only 1 for loop with a constant time work in each iteration.
  • Space complexity: $O(1)$ we don’t utilize any additional data structures and only store a variable.

Code

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        profit = 0
        for i in range(1, len(prices)):
            if prices[i] > prices[i-1]:
                profit += prices[i] - prices[i-1]

        return profit