Given a triangle array, return the minimum path sum from top to bottom.
For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.
Example 1:
Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]] Output: 11 Explanation: The triangle looks like: 2 3 4 6 5 7 4 1 8 3 The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
Example 2:
Input: triangle = [[-10]] Output: -10
Constraints:
1 <= triangle.length <= 200triangle[0].length == 1triangle[i].length == triangle[i - 1].length + 1-104 <= triangle[i][j] <= 104O(n) extra space, where n is the total number of rows in the triangle?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine standing at the top of a pyramid of numbers and trying to find a path down to the bottom that adds up to the smallest possible total. The brute force approach works by exploring every single possible pathway from the top to the bottom.
Here's how the algorithm would work step-by-step:
def minimum_total_brute_force(triangle_grid):
# Explore all possible paths recursively to evaluate every unique route to the bottom
def explore_paths(row_index, element_index):
if row_index == len(triangle_grid):
return 0
current_value = triangle_grid[row_index][element_index]
left_path_sum = explore_paths(row_index + 1, element_index)
right_path_sum = explore_paths(row_index + 1, element_index + 1)
# Choose the path yielding the smaller sum to bubble up the optimal local decision
return current_value + min(left_path_sum, right_path_sum)
# Initiate the exhaustive search starting from the very top peak of the triangle
return explore_paths(0, 0)Instead of trying to figure out the best path by starting at the very top and guessing blindly, the smartest strategy works backward from the very bottom. By looking at the last row first and making cheap local choices as you move upward, you naturally uncover the best overall path without having to check every single combination.
Here's how the algorithm would work step-by-step:
def minimum_total(triangle_matrix):
# Working bottom-up avoids redundant path calculations and exponential time complexity.
for row_index in range(len(triangle_matrix) - 2, -1, -1):
for element_index in range(len(triangle_matrix[row_index])):
# Choosing the smaller local option ensures the global sum remains optimal.
triangle_matrix[row_index][element_index] += min(
triangle_matrix[row_index + 1][element_index],
triangle_matrix[row_index + 1][element_index + 1]
)
# The top element accumulates the absolute minimum path sum from the entire traversal.
return triangle_matrix[0][0]| Case | How to Handle |
|---|---|
| Empty triangle or null input | Return zero immediately as the minimum path sum for an empty structure is undefined or zero. |
| Triangle with only a single row containing one element | Return that single element directly without performing any additional row transitions. |
| Triangle containing negative numbers | Ensure the dynamic programming recurrence relation correctly accumulates negative values to yield a smaller path sum. |
| Asymmetric or ragged triangular structures | Validate that each row length strictly equals its row index plus one to prevent out-of-bounds indexing. |
| Very large triangle heights approaching maximum constraints | Use bottom-up dynamic programming with O(n) space optimization to prevent stack overflow and memory limit issues. |
| All elements in the triangle are identical | The algorithm correctly computes the cumulative path by continuously adding the node value to the minimum of its children. |
| Extreme boundary elements on the far left or right edges | Handle boundary conditions explicitly by only considering a single adjacent child rather than two. |
| Very large positive numbers causing potential integer overflow | Ensure the data types used for accumulation can hold values exceeding standard 32-bit integer limits if inputs are large. |