LeetCode Problem

How to Solve Triangle

This problem asks you to find the minimum path sum from the top to the bottom of a triangle. By using dynamic programming with state transitions, you can calculate the optimal path that minimizes the sum. The most efficient solution is to solve this problem by modifying the triangle in place, reducing both time and space complexity.

GhostInterview Help

Need help with Triangle without spending extra time grinding it?

GhostInterview can read Triangle from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #120State transition dynamic programmingReviewed 2026-03-08
Difficulty
Medium
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem asks you to find the minimum path sum from the top to the bottom of a triangle. By using dynamic programming with state transitions, you can calculate the optimal path that minimizes the sum. The most efficient solution is to solve this problem by modifying the triangle in place, reducing both time and space complexity.

Problem Statement

You are given a triangle represented as a 2D array. The task is to find the minimum path sum from top to bottom. At each step, you can move to an adjacent number in the row directly below.

More specifically, if you're currently at index i in the row, you can move either to index i or index i+1 in the next row. Your goal is to return the minimum sum of the path that starts from the top and ends at the bottom.

Examples

Example 1

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

Output: 11

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

Example details omitted.

Constraints

  • 1 <= triangle.length <= 200
  • triangle[0].length == 1
  • triangle[i].length == triangle[i - 1].length + 1
  • -104 <= triangle[i][j] <= 104

Solution Approach

Bottom-Up Dynamic Programming

Start from the second-to-last row and move upwards, updating each element with the minimum sum path from that element to the bottom. This modifies the triangle in place, reducing space complexity.

State Transition Optimization

By focusing on state transitions, we can optimize the solution by updating only two rows at a time. This reduces both space and time complexity, as we no longer need to store the entire triangle.

Iterative Bottom-Up Approach

An alternative bottom-up approach involves using an array to store the minimum sums from the bottom of the triangle. This avoids altering the triangle itself, maintaining original input integrity.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity of the solution is O(n) because we process each element once, where n is the total number of elements in the triangle. The space complexity is O(n) for the iterative approach using an auxiliary array, or O(1) if modifying the triangle in place.

What Interviewers Usually Probe

  • The candidate demonstrates strong understanding of dynamic programming by recognizing the optimal bottom-up approach.
  • The candidate effectively reduces space complexity by updating the triangle in place or using a two-row technique.
  • The candidate struggles with the pattern of state transition optimization or proposes inefficient solutions.

Common Pitfalls or Variants

Common pitfalls

  • Over-complicating the problem by using a top-down approach, which leads to redundant calculations and higher time complexity.
  • Not correctly handling edge cases, such as when the triangle has only one element.
  • Confusing the minimum sum path with a simple greedy approach, which may not yield the correct result.

Follow-up variants

  • Modify the problem to allow only movements to the left or right, rather than adjacent numbers.
  • Extend the problem to find the maximum path sum instead of the minimum.
  • Solve the problem with a variant where the triangle elements are non-integer values, such as decimals or negative numbers.

How GhostInterview Helps

  • GhostInterview helps by providing step-by-step guidance on breaking down the dynamic programming approach for this problem, ensuring you understand the state transition process.
  • It assists by helping you identify when to optimize space and time complexity in a state transition problem, allowing you to implement efficient solutions.
  • GhostInterview offers real-time feedback on your approach, pointing out common pitfalls and suggesting alternatives when you struggle with efficiency.

Topic Pages

FAQ

How do I approach the Triangle problem with dynamic programming?

Start from the bottom of the triangle and update each element by adding the minimum path sum from the row directly below. This is the key to the bottom-up approach.

Can I use a top-down approach for the Triangle problem?

While a top-down approach works, it is less efficient because it involves redundant calculations. The bottom-up approach is recommended for better performance.

What is the optimal space complexity for the Triangle problem?

The optimal space complexity is O(1) if you modify the triangle in place or O(n) if you use an auxiliary array to store intermediate results.

What is the time complexity of solving the Triangle problem using dynamic programming?

The time complexity is O(n), where n is the total number of elements in the triangle, because each element is processed only once.

What are some common mistakes when solving the Triangle problem?

Common mistakes include over-complicating the solution with a top-down approach, incorrectly handling edge cases, and confusing greedy strategies with dynamic programming.

GhostInterview Solver

Need direct help with Triangle instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Triangle from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.