**一. 题目描述**
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.
**二. 题目分析**
使用动态规划来完成。设从顶部到第`i`层的第`k`个顶点的最小路径长度表示为`f(i, k)`,则`f(i, k) = min{f(i-1,k), f(i-1,k-1)} + d(i, k)`,其中`d(i, k)`表示原来三角形数组里的第i行第k列的元素。则可以求得从第一行到最终到第`length-1`行第`k`个元素的最小路径长度,最后再比较第`length-1`行中所有元素的路径长度大小,求得最小值。
这里需要注意边界条件,即每一行中的第一和最后一个元素在上一行中只有一个邻居。而其他中间的元素在上一行中都有两个相邻元素。
**三. 示例代码**
~~~
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
vector< vector<int> >::size_type length = triangle.size();
if(length == 0){
return 0;
}
int i, j;
for(i=1;i<length;i++){
vector<int>::size_type length_inner = triangle[i].size();
for(j=0;j<length_inner;j++){
if(j == 0){
triangle[i][j] = triangle[i][j] + triangle[i-1][j];
}
else if(j == length_inner - 1){
triangle[i][j] = triangle[i][j] + triangle[i-1][j-1];
}
else{
triangle[i][j] = (triangle[i][j] + triangle[i-1][j-1] < triangle[i][j] + triangle[i-1][j] ? triangle[i][j] + triangle[i-1][j-1]:triangle[i][j] + triangle[i-1][j]);
}
}
}
int min_path = triangle[length-1][0];
for(i=1;i<triangle[length-1].size();i++){
min_path = (min_path < triangle[length-1][i]?min_path:triangle[length-1][i]);
}
return min_path;
}
};
~~~
**四. 小结**
无
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching