Skip to main content
  1. LeetCode/

LeetCode 120: Triangle

·3 mins· ·
LeetCode Medium Dynamic-Programming
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-06-27 Source: Day 30 learning note

Study Context
#

This article is rebuilt from the exact LeetCode section in the learning note. I kept the note’s repair points, comparison points, and common mistakes, while removing unrelated non-LeetCode material from the same day.

Related Reminders From The Note#

  • explain LC 120 as in-place DP with exact state meaning after overwrite
  • walk the edge cases for LC 120 left edge, right edge, and middle cells

Learning Note Extract
#

Problem 2 - LC 120 Triangle
#

  • Pattern: DP on jagged rows / in-place bottom-up accumulation.

Why This Fits
#

Each position in row r depends only on legal parents from row r - 1.

The row shape is not rectangular, but the dependency is still local and acyclic, so DP fits cleanly.

Core State / Invariant
#

For the in-place version:

triangle[r][c] = minimum path sum to reach position (r, c) after update

Edge Rules
#

Left edge:

c == 0

Can only come from:

triangle[r - 1][0]

Right edge:

c == r

Can only come from:

triangle[r - 1][c - 1]

Middle cells:

triangle[r][c] += min(triangle[r - 1][c - 1], triangle[r - 1][c])

Final Answer
#

After all updates:

answer = min(triangle[last_row])

Because any position in the last row can be the endpoint of a valid top-to-bottom path.

Complexity
#

Time: O(total cells)
Space: O(1) extra

For n rows:

Time: O(n^2)

Common Mistakes
#

  • thinking in-place update means it is not DP
  • forgetting the left and right edges each have only one legal parent
  • saying every cell has two parents
  • giving vague complexity like O(n * m) when the structure is a triangle, not a rectangle

Strong Spoken Explanation
#

I update the triangle in place so that each entry becomes the minimum path sum to reach that position. The left edge has only one parent directly above, the right edge has only one parent above-left, and middle cells can come from either of the two parents in the previous row. After processing all rows, the minimum answer is the minimum value in the last row.

Clean Solution
#

The note above captures the reasoning and the mistakes to avoid. The implementation below is the version I would submit.

from typing import List

class Solution:
    def minimumTotal(self, triangle: List[List[int]]) -> int:
        dp = triangle[-1][:]

        for r in range(len(triangle) - 2, -1, -1):
            for c in range(len(triangle[r])):
                dp[c] = triangle[r][c] + min(dp[c], dp[c + 1])

        return dp[0]

Complexity
#

Time O(number of cells), Space O(width of last row).

Mistakes To Watch
#

  • Trying to greedily choose the smaller child at each row.
  • Forgetting that row lengths change.

Final Interview Explanation
#

Start from the state definition, then explain why the transition preserves that state. If there is a loop direction, state compression, or a similar-looking problem with a different answer shape, call that out explicitly because that is where this problem family usually breaks down.

Related

LeetCode 63: Unique Paths II
·3 mins
LeetCode Medium Dynamic-Programming Grid-Dp
LeetCode note for Unique Paths II, rebuilt from the original learning note
LeetCode 376: Wiggle Subsequence
·3 mins
LeetCode Medium Dynamic-Programming Greedy
LeetCode note for Wiggle Subsequence, rebuilt from the original learning note
LeetCode 714: Best Time to Buy and Sell Stock with Transaction Fee
·3 mins
LeetCode Medium Dynamic-Programming State-Machine
LeetCode note for Best Time to Buy and Sell Stock with Transaction Fee, rebuilt from the original learning note
LeetCode 62: Unique Paths
·3 mins
LeetCode Medium Dynamic-Programming Grid-Dp
LeetCode note for Unique Paths, rebuilt from the original learning note
LeetCode 64: Minimum Path Sum
·3 mins
LeetCode Medium Dynamic-Programming Grid-Dp
LeetCode note for Minimum Path Sum, rebuilt from the original learning note
LeetCode 276: Paint Fence
·3 mins
LeetCode Medium Dynamic-Programming
LeetCode note for Paint Fence, rebuilt from the original learning note