Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Triangle problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Triangle problem solution, we have 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.

Leetcode Triangle problem solution

Leetcode Triangle problem solution in Python.

def minimumTotal(self, triangle):
    m = len(triangle)
    n = len(triangle[-1])
    dp = [i for i in triangle[-1]]
    
    for i in range(m - 2, -1, -1):
        for j in range(len(triangle[i])):                
            dp[j] = min(dp[j], dp[j + 1]) + triangle[i][j]
        
    return dp[0]

Triangle problem solution in Java.

class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        if(triangle.size() == 0 || triangle.get(0).size() == 0)
            return 0;
        int n = triangle.size();
        int[] dp = new int[n];
        for(int i = 0; i < n; i++)
            dp[i] = triangle.get(n-1).get(i);
        
        for(int i = n-2; i >= 0; i--){
            for(int j = 0; j <= i; j++){
                dp[j] = Math.min(dp[j], dp[j+1]) + triangle.get(i).get(j);
            }
        }
        return dp[0];
    }
}

Problem solution in C++.

class Solution {
public:

int minimumTotal(vector<vector<int>>& triangle) {
 int n=triangle.size();
 if(n==0) return 0;
 if(n==1) return triangle[0][0];
 int mine=INT_MAX;
 for(int i=1;i<n;i++)
 {
     for(int j=0;j<=i;j++)
     {
       if(j-1>=0&&j<=i-1)
          triangle[i][j]+=min(triangle[i-1][j],triangle[i-1][j-1]);
       else if(j>i-1&&j-1>=0)
           triangle[i][j]+=triangle[i-1][j-1];
       else if(j-1<0)
           triangle[i][j]+=triangle[i-1][j];
       
       if(i==n-1)
           mine=min(mine,triangle[i][j]);
     }
 }
    return mine;
}
};

Problem solution in C.

int minimumTotal(int** triangle, int triangleRowSize, int *triangleColSizes) {
    int i,j;
    if(triangleRowSize == 0) return 0;
    for(i=triangleRowSize-2;i>=0;i--){
        for(j=0;j<triangleColSizes[i];j++){
            if(triangle[i+1][j] < triangle[i+1][j+1]){
                triangle[i][j] += triangle[i+1][j];
            } else {
                triangle[i][j] += triangle[i+1][j+1];
            }
        }
    }
    return triangle[0][0];
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes