Paths Subjects Questions Quizzes Pricing Search
Intermediate Open Pro

Triangle Numbers

You are given a triangle represented as a list of lists, where triangle[i] has i + 1 integers and row i sits directly below row i - 1 (each element connects to the two elements diagonally below it in the next row). Starting from the top, find the minimum possible sum of a path that moves to an adjacent number in the row below at each step, ending on the bottom row.

Formally, if you are at index j in row i, you may move to index j or index j + 1 in row i + 1.

Example 1

Input: triangle = [[2],
                    [3, 4],
                    [6, 5, 7],
                    [4, 1, 8, 3]]
Output: 11
Explanation: The minimum path is 2 -> 3 -> 5 -> 1 = 11.

Example 2

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

Constraints

  • 1 <= triangle.length <= 200
  • triangle[0].length == 1
  • triangle[i].length == triangle[i - 1].length + 1
  • -10^4 <= triangle[i][j] <= 10^4

Share this question

← Back to Math and Geometry practice

We use cookies for product analytics to improve OmniAtlas. See our Privacy Policy.