Find the minimum top-to-bottom path sum in a triangle

Quick Overview

Find the smallest sum of a path from the top of a number triangle to its bottom row, moving to one of two adjacent entries in the next row at each step. Tests bottom-up dynamic programming, keeping only one row of state, and spotting why a greedy choice fails.

Find the minimum top-to-bottom path sum in a triangle

Company: PayPal

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a `triangle` array of integers, return the minimum path sum from top to bottom. For each step, you may move to an adjacent number of the row below. More formally, if you are on index `i` on the current row, you may move to either index `i` or index `i + 1` on the next row. ### Function Signature ```python def minimum_total(triangle: list[list[int]]) -> int: ``` ### Rules - The triangle is a list of rows. Row `r` holds `r + 1` integers. - A path starts at `triangle[0][0]`, takes exactly one value from every row, and ends in the bottom row. - Return the sum of the values on the cheapest path. Only the sum is returned, so the answer is unique even when several paths tie. ### Constraints - `1 <= len(triangle) <= 200` - `len(triangle[0]) == 1` - `len(triangle[i]) == len(triangle[i - 1]) + 1` - `-10^4 <= triangle[i][j] <= 10^4` ### Examples ```text Input: triangle = [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]] Output: 11 2 / \ 3 4 / \ / \ 6 5 7 / \ / \ / \ 4 1 8 3 ``` The minimum path is 2 -> 3 -> 5 -> 1, which sums to 11. Every other path costs at least 12. ```text Input: triangle = [[-10]] Output: -10 ``` The triangle has a single value, so the only path is that value. ```text Input: triangle = [[1], [2, 3], [9, 9, -10]] Output: -6 ``` Taking the smaller value 2 in the second row only reaches the two 9s, for a total of 12. The best path takes 3 so it can reach -10: 1 -> 3 -> -10 = -6.

Overview: Find the smallest sum of a path from the top of a number triangle to its bottom row, moving to one of two adjacent entries in the next row at each step. Tests bottom-up dynamic programming, keeping only one row of state, and spotting why a greedy choice fails.

PayPal logo
PayPal
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a triangle array of integers, return the minimum path sum from top to bottom.

For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.

Function Signature

def minimum_total(triangle: list[list[int]]) -> int:

Rules

  • The triangle is a list of rows. Row r holds r + 1 integers.
  • A path starts at triangle[0][0] , takes exactly one value from every row, and ends in the bottom row.
  • Return the sum of the values on the cheapest path. Only the sum is returned, so the answer is unique even when several paths tie.

Constraints

  • 1 <= len(triangle) <= 200
  • len(triangle[0]) == 1
  • len(triangle[i]) == len(triangle[i - 1]) + 1
  • -10^4 <= triangle[i][j] <= 10^4

Examples

Input: triangle = [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]]
Output: 11

       2
      / \
     3   4
    / \ / \
   6   5   7
  / \ / \ / \
 4   1   8   3

The minimum path is 2 -> 3 -> 5 -> 1, which sums to 11. Every other path costs at least 12.

Input: triangle = [[-10]]
Output: -10

The triangle has a single value, so the only path is that value.

Input: triangle = [[1], [2, 3], [9, 9, -10]]
Output: -6

Taking the smaller value 2 in the second row only reaches the two 9s, for a total of 12. The best path takes 3 so it can reach -10: 1 -> 3 -> -10 = -6.