Skip to the code
+10 XP

#507 Cheapest Path Down a Pyramid Chart

easy

A pyramid chart has rows of numbers: row 0 has one number, row 1 has two, and so on. From position i in one row, you may step to position i or i + 1 in the next row.

Write cheapestDescent(rows) that returns the smallest sum of numbers on a path from the top to any number in the bottom row.

Example 1

Input: rows = [[3],[7,4],[2,4,6],[8,5,9,3]]
Output: 16

3 → 4 → 4 → 5, or 3 → 4 → 6 → 3.

Example 2

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

A single number.

Constraints

  • 1 <= rows.length <= 200
  • rows[r].length === r + 1
  • -10000 <= rows[r][i] <= 10000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).