标签:
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[ [2], [3,4], [6,5,7], [4,1,8,3] ]
The minimum path sum from top to bottom is 11
(i.e., 2 + 3 + 5 + 1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
这个问题分为两种情况,一种不可以修改原数组元素,另一种可以修改数组元素
1、可以修改数组元素
1 class Solution { 2 public: 3 int minimumTotal(vector<vector<int>>& triangle) { 4 for(int i = triangle.size()-2; i >= 0; --i) 5 { 6 for(int j = 0; j < i+1;++j) 7 { 8 if(triangle[i+1][j]<triangle[i+1][j+1]) 9 triangle[i][j] += triangle[i+1][j]; 10 else 11 triangle[i][j] += triangle[i+1][j+1]; 12 } 13 14 } 15 return triangle[0][0]; 16 } 17 };
2、不能修改数组元素
LeetCode -- Triangle 路径求最小和( 动态规划问题)
标签:
原文地址:http://www.cnblogs.com/chdxiaoming/p/4775755.html