#120

Triangle

Medium Bottom-up DP

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Find the minimum path sum from the top to the bottom of a number triangle, moving to an adjacent number on the row below each step.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the smallest top-to-bottom path sum in a triangle.

Why this works: go bottom-up: each cell becomes its value + the smaller of its two children.

Example: [[2],[3,4],[6,5,7],[4,1,8,3]]

row 2 becomes [7, 6, 10]row 1 becomes [9, 10]top 2 + 9 = 11

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Greedily picking the smaller child at each step.

Solve on LeetCode ↗
Complexity
  • Time: O(n²) for n rows.
  • Space: O(n).
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon