/120. Minimum Path Sum in Triangle Array

120. Minimum Path Sum in Triangle Array

Medium
Arrays59.9% acceptance

Given a list of lists of integers named triangle_matrix, where triangle_matrix[i] has i+1 elements, compute the minimum sum of a path from the top element to any element in the last row. At each step, you may move to either the same index or the next index in the row below. Return the minimum path sum as an integer.

Example 1

Input: [[1],[2,3],[3,6,7],[4,5,8,9]]

Output: 10

Explanation: Path: 1 -> 2 -> 3 -> 4 = 10

Example 2

Input: [[5],[6,7],[8,9,10]]

Output: 19

Explanation: Path: 5 -> 6 -> 8 = 19

Example 3

Input: [[0],[1,2],[3,4,5]]

Output: 4

Explanation: Path: 0 -> 1 -> 3 = 4

Constraints

  • 1 <= len(triangle_matrix) <= 200
  • triangle_matrix[0] has length 1
  • triangle_matrix[i] has length i+1 for all 0 <= i < len(triangle_matrix)
  • -104 <= triangle_matrix[i][j] <= 104 for all valid i, j
Python (current runtime)

Case 1

Input: [[3],[1,2],[4,5,6],[7,8,9,10]]

Expected: 15

Case 2

Input: [[10],[5,6],[2,3,4],[1,2,3,4]]

Expected: 18

Case 3

Input: [[7],[3,8],[8,1,0],[2,7,4,3]]

Expected: 11