Tensorum
LeetCode 120 · Triangle / Pascal

Triangle

LeetCode 120 · Medium · solution, intuition & step-by-step walkthrough

dp[row][col] = triangle[row][col] + min(dp[row+1][col], dp[row+1][col+1]).

MediumArrayDynamic Programming
8 msruntime · beats 18%
20.8 MBmemory · beats 14%
2approaches, in order
11accepted runs · last 18 May 2026

This is how I solved Triangle (LeetCode 120), a medium problem from the Triangle / Pascal pattern. Below: the intuition, the approach step by step, my accepted Python code and the 2 versions I wrote on the way, animated test cases and a line-by-line debugger.

Intuition

From any cell you can step to the same index or the next index in the row below. Working from the bottom up is cleanest: the best path from a cell is its value plus the better of the two cells beneath it, and the top cell ends up holding the answer.

Approach

  1. 1

    Top-down: dfs(row, col) returns the cheapest path to the bottom; the last row returns its own value.

  2. 2

    Bottom-up: copy the last row into dp, then fill rows upward using the two children.

  3. 3

    dp[0][0] is the answer.

How I solved it · from first idea to final

1
Recursion + memoO(n²) time · O(n²) space

lru_cache dfs from the apex.

2
Bottom-up tabulationAcceptedO(n²) time · O(n²) space

Fill dp from the last row upward.

Recursion + memoPythonO(n²) time · O(n²) space
from functools import lru_cache
class Solution:
    def minimumTotal(self, triangle: List[List[int]]) -> int:
        if not triangle:
            return 0
        n = len(triangle)
        @lru_cache(maxsize=None)
        def dfs(row,col):
            if row>=n or col>=len(triangle[row]):
                return float("inf")
            if row==n-1:
                return triangle[n-1][col]
            step1 = dfs(row+1,col)
            step2 = dfs(row+1,col+1)
            return triangle[row][col]+min(step1,step2)
        return dfs(0,0)
Bottom-up tabulationPythonO(n²) time · O(n²) space
class Solution:
    def minimumTotal(self, triangle: List[List[int]]) -> int:
        if not triangle:
            return 0
        n = len(triangle)
        dp= [[0]*n for _ in range(n)]
        m = len(triangle[n-1])
        for col in range(m):
            dp[n-1][col]=triangle[n-1][col]
        for row in range(n-2,-1,-1):
            for col in range(len(triangle[row])):
                step1 = dp[row+1][col]
                step2 = float("inf")
                if col + 1 < len(triangle[row + 1]):
                    step2 = dp[row+1][col+1]
                dp[row][col]=triangle[row][col]+min(step1,step2)
        return dp[0][0]

s = Solution()
##test case 1 - null triangle
assert s.minimumTotal([])==0
#test case 2 - same no of columns in next row
assert s.minimumTotal([[2],[3]])==5
#test case 3 - None triangle
assert s.minimumTotal(None)==0
#test case 4 - Single No
assert s.minimumTotal([[5]])==5
Loading the step-through…

Triangle — FAQ

What is the intuition behind Triangle?

From any cell you can step to the same index or the next index in the row below. Working from the bottom up is cleanest: the best path from a cell is its value plus the better of the two cells beneath it, and the top cell ends up holding the answer.

How do you solve Triangle (LeetCode 120)?

1. Top-down: dfs(row, col) returns the cheapest path to the bottom; the last row returns its own value. 2. Bottom-up: copy the last row into dp, then fill rows upward using the two children. 3. dp[0][0] is the answer.

What is the time and space complexity of Triangle?

Time: O(n²). Space: O(n²) (one row would do). (Accepted approach: Bottom-up tabulation.)

What are common mistakes in Triangle?

Going bottom-up removes the need to take a min over the last row at the end. Guard empty or None input.

Is there more than one way to solve Triangle?

Recursion + memo (O(n²) time · O(n²) space): lru_cache dfs from the apex. Bottom-up tabulation (O(n²) time · O(n²) space): Fill dp from the last row upward.