Bird
Raised Fist0
Interview Prepdp-grid-intervalsmediumAmazonMicrosoft

Triangle (Min Path Sum)

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

Initialize dp with last row

We start by copying the last row of the triangle into the dp array. This represents the base case where the minimum path sum starting at the bottom row is the element itself.

💡 Initializing dp with the last row sets the foundation for bottom-up calculation, as these values are known and do not depend on any further computations.
Line:dp = triangle[-1][:]
💡 The dp array now holds the minimum path sums for the bottom row, which are just the values themselves.
📊
Triangle (Min Path Sum) - Watch the Algorithm Execute, Step by Step
Watching the algorithm step-by-step reveals how dynamic programming builds solutions from smaller subproblems, making the minimum path sum clear without complex recursion.
Step 1/11
·Active fillAnswer cell
Initializing dp array
i\w0
i=02
i=134
i=2657
i=34183
dp init
Initializing dp array
i\w0
i=02
i=134
i=2657
i=34183
processing row 2
Item 0 - wt:6 val:7
i\w0
i=02
i=134
i=2757
i=34183
updating dp[0]
Item 1 - wt:5 val:6
i\w0
i=02
i=134
i=2767
i=34183
updating dp[1]
Item 2 - wt:7 val:10
i\w0
i=02
i=134
i=27610
i=34183
updating dp[2]
Initializing dp array
i\w0
i=02
i=134
i=27610
i=34183
processing row 1
Item 0 - wt:3 val:9
i\w0
i=02
i=194
i=27610
i=34183
updating dp[0]
Item 1 - wt:4 val:10
i\w0
i=02
i=1910
i=27610
i=34183
updating dp[1]
Initializing dp array
i\w0
i=02
i=1910
i=27610
i=34183
processing top row
Item 0 - wt:2 val:11
i\w0
i=011
i=1910
i=27610
i=34183
final dp[0]
Item 0 - wt:2 val:11
i\w0
i=011
i=1910
i=27610
i=34183
answer cell

Key Takeaways

Bottom-up DP builds the solution from the known base case at the bottom row upwards.

This insight is hard to see from code alone because the recursion is implicit; watching dp update clarifies the process.

Each dp cell depends only on the two adjacent cells below, showing the overlapping subproblems clearly.

Visualizing dependencies helps understand why the algorithm iterates bottom-up and how it avoids recomputation.

The final answer is stored in dp[0] after processing the top row, representing the minimal path sum from apex to base.

Seeing the final dp array state confirms how the answer emerges from incremental updates.

Practice

(1/5)
1. You are given a string representing a sequence of colored balls on a board and a multiset of balls in hand. Your goal is to clear the board by inserting balls from your hand such that any group of three or more consecutive balls of the same color is removed, possibly triggering chain reactions. Which algorithmic approach guarantees finding the minimum number of insertions needed to clear the board optimally?
easy
A. Interval dynamic programming with memoization exploiting optimal substructure
B. Topological sorting of ball groups followed by greedy removals
C. Pure brute force recursion without memoization exploring all insertions
D. Greedy insertion of balls at the earliest possible position to remove groups

Solution

  1. Step 1: Understand problem structure

    The problem requires minimizing insertions to clear the board, which involves exploring overlapping subproblems and optimal substructure.
  2. Step 2: Identify suitable algorithm

    Interval DP with memoization efficiently explores all intervals and hand states, guaranteeing minimal moves by considering all splits and removals.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Interval DP is known for solving interval removal problems optimally [OK]
Hint: Interval DP handles overlapping subproblems optimally [OK]
Common Mistakes:
  • Assuming greedy insertion always yields minimal moves
  • Using brute force without memoization leads to exponential time
  • Misapplying topological sort which is unrelated here
2. Consider the following snippet from the optimal Zuma Game solution. Given board = "WRRBBW" and hand = "RB", what is the return value of findMinStep(board, hand)?
easy
A. 2
B. -1
C. 3
D. 1

Solution

  1. Step 1: Trace initial calls with board="WRRBBW" and hand="RB"

    Hand has 1 R and 1 B. The board requires more balls to clear groups of 3, but hand is insufficient to clear all.
  2. Step 2: Check if any insertion sequence clears board

    Trying to insert R or B balls to form groups of 3 fails due to insufficient balls; no sequence clears the board fully.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Return -1 indicates no solution with given hand [OK]
Hint: Insufficient hand balls cause no solution -> -1 [OK]
Common Mistakes:
  • Assuming partial removals suffice to clear board
  • Miscounting balls needed for group removal
  • Forgetting to restore hand counts after recursion
3. Consider the following buggy code for Paint House (K Colors). Which line contains the subtle bug that can cause adjacent houses to have the same color, violating constraints?
medium
A. Line with 'min_prev = min(dp[x] for x in range(k))'
B. Line with 'dp = costs[0][:]' initialization
C. Line with 'new_dp = [0]*k' inside the loop
D. Line with 'return min(dp)' at the end

Solution

  1. Step 1: Identify constraint violation

    The problem requires that adjacent houses cannot have the same color. The code must exclude the current color when computing min_prev.
  2. Step 2: Locate bug in min_prev calculation

    The line 'min_prev = min(dp[x] for x in range(k))' includes the current color's dp value, allowing same color adjacency, violating constraints.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Excluding current color in min calculation is essential [OK]
Hint: Check if min excludes current color to avoid same-color adjacency [OK]
Common Mistakes:
  • Including current color in min calculation
  • Incorrect dp initialization
  • Returning min(dp) without proper updates
4. Suppose the problem is modified so that you can reuse the same color for adjacent houses, but you want to minimize the total cost. Which modification to the DP solution is correct?
hard
A. Use brute force recursion without memoization since constraints are relaxed.
B. Keep excluding the previous color but add a penalty cost for same color adjacency.
C. Remove the condition excluding the previous color when computing min_prev; just take min(dp) for all colors.
D. Use a greedy approach picking the cheapest color for each house independently.

Solution

  1. Step 1: Understand constraint relaxation

    Allowing same color for adjacent houses removes the need to exclude previous color in DP transitions.
  2. Step 2: Modify DP accordingly

    Now min_prev is simply min(dp) over all colors, no exclusion needed. This simplifies the DP and still finds minimum cost.
  3. Step 3: Evaluate other options

    Adding penalty is unnecessary. Brute force is inefficient. Greedy fails because costs vary per house and color.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Relaxed constraints allow including all colors in min calculation [OK]
Hint: Relaxed constraints mean no exclusion of previous color in DP [OK]
Common Mistakes:
  • Still excluding previous color unnecessarily
  • Adding penalty complicates solution
  • Using brute force or greedy incorrectly
5. Suppose the Strange Printer problem is modified so that the printer can reuse previously printed characters anywhere in the string without reprinting them. Which modification to the DP approach correctly handles this variant?
hard
A. Modify DP to consider non-contiguous intervals and allow merging non-adjacent segments
B. Add a global visited set to track printed characters and skip them
C. No change needed; original DP already accounts for reuse
D. Use a greedy approach printing each unique character once

Solution

  1. Step 1: Understand reuse variant

    Reusing printed characters anywhere means intervals are no longer strictly contiguous; DP must consider merging non-adjacent segments.
  2. Step 2: Identify correct DP modification

    Original DP assumes contiguous intervals. To handle reuse, DP must be extended to track sets of printed characters or allow merging non-contiguous intervals, which requires more complex state representation.
  3. Step 3: Evaluate options

    No change (C) ignores new reuse property. Global visited set (B) is insufficient for interval merging. Greedy (D) fails on complex merges. Only (A) correctly adapts DP for non-contiguous merges.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Reuse breaks contiguous assumption; DP must handle non-adjacent merges [OK]
Hint: Reuse breaks contiguous intervals; DP must track complex merges [OK]
Common Mistakes:
  • Assuming original DP handles reuse without changes
  • Trying greedy approach