30 Days of LeetCode: Day 8 - When Building on Previous Solutions Backfires

30 Days of LeetCode: Day 8 - When Building on Previous Solutions Backfires

Today's LeetCode challenge taught me an important lesson about pattern recognition: sometimes seeing similarities to a previous problem can lead you down the wrong path. While yesterday's stock trading problem and today's shared some characteristics, trying to adapt yesterday's solution made things unnecessarily complex.

The Challenge

Given an array of stock prices, find the maximum profit you can achieve by buying and selling multiple times. You can only hold one share at a time, but you can buy and sell on consecutive days.

Example:

Input: prices = [7,1,5,3,6,4]
Output: 7
// Buy at 1, sell at 5 (profit of 4)
// Buy at 3, sell at 6 (profit of 3)
// Total profit = 7

My Initial Approach: Building on Yesterday

Fresh from yesterday's success with tracking minPrice and maxProfit, I tried to adapt that solution:

var maxProfit = function(prices) {
    let minPrice = prices[0];
    let maxProfit = 0;
    const profitArr = [];  // Added this to track multiple profits

    for(let i = 1; i < prices.length; i++) {
        if(prices[i] - minPrice > maxProfit) {
            maxProfit = prices[i] - minPrice;
            profitArr.push(maxProfit)  // Store each profit
        }
        if(prices[i] < minPrice) {
            minPrice = prices[i];
        }
    }

    return profitArr.reduce((a, b) => a + b, 0);
};

I recognized we could make multiple trades, so I thought: "I'll just store each profit in an array and sum them up!"

Why It Failed

My approach failed because:

  1. I was still looking for minimum prices when I should have been looking at daily changes

  2. Storing profits in an array was unnecessary overhead

  3. I missed the key insight: we can profit from ANY price increase

The Simple Solution

The actual solution was much simpler:

var maxProfit = function(prices) {
    let profit = 0;

    for(let i = 1; i < prices.length; i++) {
        if(prices[i]/*current day price*/ > prices[i-1]/*previous day*/) {
            profit += prices[i] - prices[i-1];
        }
    }

    return profit;
};

The Key Insight

The breakthrough came when I realized:

  1. We don't need to track minimum prices

  2. We don't need to store profits in an array

  3. We just need to check: "Is tomorrow's price higher than today's?"

  4. If yes, we can make profit by buying today and selling tomorrow

Learning Impact

This experience taught me several valuable lessons:

  1. Similar problems don't always need similar solutions

  2. Sometimes building on previous solutions adds unnecessary complexity

  3. Look for what's different about the new problem

  4. Reoccuring lesson: the simplest solution is often the best solution

Most importantly, I learned that while pattern recognition is valuable, it shouldn't blind us to simpler approaches. Just because yesterday's problem needed tracking state doesn't mean today's does.