The problem

Say you have an array prices for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

Example 2:

Example 3:

Constraints:

  • 1 <= prices.length <= 3 * 10 ^ 4
  • 0 <= prices[i]&nbsp;<= 10 ^ 4

The solution

# Our function takes in a list of day prices
def maxProfit(prices):
    # Store our max profit
    maxprofit = 0

    # Enumerate through the price list
    for i, item in enumerate(prices):
        # Only start checking from day 2
        if i>0:
            # If the current day is more than the last
            if prices[i] > prices[i-1]:
                # Increment our profit
                maxprofit += prices[i] - prices[i-1]

    # Return the max profit integer
    return maxprofit