Skip to main content
Back to problems
#120
Medium Algorithms

Triangle

Array Dynamic Programming
59.9% acceptance
Jan 12, 2026
10812
609
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.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn minimum_total(triangle: Vec<Vec<i32>>) -> i32 {
    let mut dp = triangle[triangle.len() - 1].clone();
    
    for i in (0..triangle.len() - 1).rev() {
      for j in 0..triangle[i].len() {
        dp[j] = triangle[i][j] + dp[j].min(dp[j + 1]);
      }
    }
    
    dp[0]
  }
}