Best Time to Buy and Sell Stock II - Leetcode Solution


💡 Step-by-Step Thought Process

  1. Understand the problem: Maximize profit by buying and selling stocks multiple times, given daily prices, without holding multiple stocks simultaneously.
  2. Initialize variables: set index i to 0, lowest price (lo) and highest price (hi) to the first price, and profit to 0.
  3. While i is less than the array length minus 1, iterate to find buy and sell points.
  4. To find a buy point, increment i while the current price is greater than or equal to the next price, ensuring a local minimum.
  5. Set lo to the price at index i as the buy price.
  6. To find a sell point, increment i while the current price is less than or equal to the next price, ensuring a local maximum.
  7. Set hi to the price at index i as the sell price.
  8. Add the difference (hi - lo) to the total profit.
  9. Return the total profit after the loop ends.

Code Solution


                

                

                

                

Detailed Explanation

Understanding the Strategy for Maximum Profit

The key to solving the "Best Time to Buy and Sell Stock II" problem lies in understanding that we are allowed to perform multiple transactions (buy one and sell one share multiple times). However, we cannot hold multiple shares simultaneously. The goal is to accumulate profit from every increase in price — every upward trend between two days is a profit opportunity.

How the Greedy Algorithm Works

We iterate through the array of prices and simply add the profit from every consecutive price increase. That is, if today's price is higher than yesterday's, we "buy" yesterday and "sell" today, adding the difference to the total profit. This greedy strategy ensures that we capture every profitable opportunity without needing to track individual transactions explicitly.

Time and Space Complexity

  • Time Complexity: O(n), where n is the number of days, since we traverse the list once.
  • Space Complexity: O(1), as we use only constant extra space for tracking profit.

Why This Strategy is Optimal

Since we are allowed unlimited transactions and there are no cooldown or fee constraints, the best approach is to collect profits from every upward swing. By treating every rising pair as a potential buy-sell opportunity, we ensure no gains are missed. This greedy method is not only simple but also optimal for this problem scenario.

Conclusion

The "Best Time to Buy and Sell Stock II" problem is a classic example of when a greedy algorithm delivers an optimal solution. Recognizing the conditions — multiple transactions allowed with no additional constraints — enables a straightforward and efficient solution. This technique is particularly valuable in financial modeling, trading simulations, and competitive coding.

Get Personalized Support at AlgoMap Bootcamp 💡