Skip to content
Decoder

Triangle

Medium

Problem

A triangle of numbers has one value in row 0, two in row 1, and so on. Start at the top and move down one row at a time. From index i in a row you may step to index `i` or `i + 1` in the row below. Return the minimum path sum from top to bottom.

Example 1

triangle = [[2],

Example 2

            [3, 4],

Example 3

            [6, 5, 7],

Example 4

Input
[4, 1, 8, 3]]
Output
11
Why
2 + 3 + 5 + 1

Example 5

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

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.