35 · Dynamic Programming

#120 Triangle

Mediumsolve on LeetCode ↗

frequently reported atBloombergAmazonMicrosoftGoogleGoldman Sachs

the problem

In plain words

A triangle of numbers has 1 number in row 0, 2 in row 1, and so on. Start at the top. From index i in one row you may step to index i or i + 1 in the next row. Return the smallest possible sum of a path from the top to the bottom row.

in triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]

out 11

2 + 3 + 5 + 1.

in triangle = [[-10]]

out -10

  • 1 ≤ triangle.length ≤ 200
  • Row 0 has 1 number; each row is one longer than the row above
  • −10⁴ ≤ triangle[i][j] ≤ 10⁴
  • Follow-up: O(n) extra space, where n is the number of rows

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #64
      Minimum Path Sum
      Mediumin Dynamic Programming
    2. #62
      Unique Paths
      Mediumin Dynamic Programming