Triangle
2017-12-05 19:15
183 查看
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
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.
时间复杂度O(N),N表示triangle的元素个数,空间复杂度O(n),n表示最底层的元素个数。
为了更加深刻的理解DP的递归方案,下面提供了一种基于memo的recursive方案及相关测试代码。
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.
时间复杂度O(N),N表示triangle的元素个数,空间复杂度O(n),n表示最底层的元素个数。
class Solution { public: int minimumTotal(vector<vector<int>>& triangle) { int minimumSum = 0; if (triangle.size() >= 1) { int row = triangle.size() - 1, col = 0; vector<int> minimumRow(triangle[row].size(), 0); for (col = 0; col < triangle[row].size(); col++) { minimumRow[col] = triangle[row][col]; } for (int row = triangle.size() - 2; row >= 0; row--) { for (int col = 0; col < triangle[row].size(); col++) { if (minimumRow[col] < minimumRow[col + 1]) { minimumRow[col] = minimumRow[col] + triangle[row][col]; } else { minimumRow[col] = minimumRow[col + 1] + triangle[row][col]; } } } minimumSum = minimumRow[0]; } return minimumSum; } };
为了更加深刻的理解DP的递归方案,下面提供了一种基于memo的recursive方案及相关测试代码。
#include <algorithm> #include <iostream> #include <vector> using namespace std; int getMinPathSum(vector<vector<int>> &triangle, int row) { static vector<int> minMemo(triangle.size()); int col = 0; if (row == triangle.size() - 2) { for (col = 0; col < triangle[row].size(); col++) { minMemo[col] = min(triangle[row + 1][col], triangle[row + 1][col+1]) + triangle[row][col]; } } else { getMinPathSum(triangle, row + 1); for (col = 0; col < triangle[row].size(); col++) { minMemo[col] = min(minMemo[col], minMemo[col + 1]) + triangle[row][col]; } } return minMemo[0]; } int main(int argc, char * * argv, char * * env) { vector<int> row1 = { 2 }; vector<int> row2 = { 3, 4 }; vector<int> row3 = { 6, 5, 7 }; vector<int> row4 = { 4, 1, 8, 3 }; vector<vector<int>> triangle; triangle.push_back(row1); triangle.push_back(row2); triangle.push_back(row3); triangle.push_back(row4); int mingPathSum = getMinPathSum(triangle, 0); std::cout << "MinPathSum:" << mingPathSum << std::endl; char ch; scanf_s("%c", &ch); return 0; }
相关文章推荐
- 一个趣味几何问题:Triangle_Puzzle
- 帕斯卡三角形(Pascal's triangle)
- Triangle War--POJ 1085
- hdu 3939 Sticks and Right Triangle 求<=N的素勾股数的个数 容斥原理
- 欧拉项目 Problem 12 of What is the value of the first triangle number to have over five hundred divisors
- 《第十三周任务三》写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形)。用如下的mian()函数,求出定义的几个几何体
- poj2079 Triangle (旋转卡壳之最大三角形)
- JAVA--第五周实验--任务4--将三角形对象Triangle、梯形对象Ladder、圆形对象Circle进行测试。
- LeetCode Pascal's Triangle II
- HDU3188:Just A Triangle
- Leetcode 120 Triangle
- 【leetcode】Pascal's Triangle II
- Leetcode: Pascal's Triangle II
- [LeetCode]Pascal's Triangle
- [LeetCode 120] - 三角形(Triangle)
- [Leetcode 65] 120 Triangle
- The Rascal Triangle 题解
- [LeetCode] Triangle
- [leetcode]Triangle
- Triangle