Suppose we have a triangle. We have to find the minimum path sum from top to the bottom. In each step we can move to adjacent numbers on the row below.
For example, if the following triangle is like
[ [2], [3,4], [6,5,7], [4,1,8,3] ]
The minimum path sum from top to bottom is 11 (2 + 3 + 5 + 1 = 11).
Let us see the steps
Let us see the following implementation to get better understanding
class Solution { public: void printVector(vector <int>& v){ for(int i = 0; i < v.size(); i++)cout << v[i] << " "; cout << endl; } int minimumTotal(vector<vector<int>>& triangle) { vector <int> dp(triangle.back()); int n = triangle.size(); for(int i = n - 2; i >= 0; i--){ for(int j = 0; j <= i; j++){ dp[j] = triangle[i][j] + min(dp[j], dp[j + 1]); } // printVector(dp); } return dp[0]; } };
[[2],[3,4],[6,5,7],[4,1,8,3]]
11