算法作业_27(2017.6.8第十六周)
2017-06-08 18:39
197 查看
120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjachttp://write.blog.csdn.net/posteditent numbers on the row below.
For example, given the following triangle
The minimum path sum from top to bottom is
11).
解析:动态规划的题目,求一个三角形二维数组从顶端到地段的最小路径。我们可以从底端(倒数第二行)开始,采用又底向上的方法。
状态转移方程为:dp[i][j] = min(dp[i+1][j] + dp[i+1][j+1]) +dp[i][j]。dp[0][0]就是答案,代码如下:
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int size = triangle.size();
for(int i = size-2;i>=0;i--){
for(int j = 0;j<=i;j++){
triangle[i][j] = min(triangle[i+1][j],triangle[i+1][j+1])+triangle[i][j];
}
}
return triangle[0][0];
}
};
Add to List
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjachttp://write.blog.csdn.net/posteditent 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).
解析:动态规划的题目,求一个三角形二维数组从顶端到地段的最小路径。我们可以从底端(倒数第二行)开始,采用又底向上的方法。
状态转移方程为:dp[i][j] = min(dp[i+1][j] + dp[i+1][j+1]) +dp[i][j]。dp[0][0]就是答案,代码如下:
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int size = triangle.size();
for(int i = size-2;i>=0;i--){
for(int j = 0;j<=i;j++){
triangle[i][j] = min(triangle[i+1][j],triangle[i+1][j+1])+triangle[i][j];
}
}
return triangle[0][0];
}
};
Add to List
120. Triangle
相关文章推荐
- 算法设计与结构基础作业第十六周
- 算法作业_26(2017.6.6第十六周)
- 算法课第十六周作业 | Single Number
- 算法第十六周作业01
- 算法作业19
- 作业排程算法—动态规划实现
- 微软等数据结构+算法面试100题(27)--颠倒栈
- 操作系统进程调度算法 先到先服务 短作业 优先级 时间片轮转
- 【莫队算法】【权值分块】bzoj3236 [Ahoi2013]作业
- 北航算法作业一 约瑟夫环问题
- 算法第十七周作业01
- 算法设计与应用基础:第十六周(1)
- 第十六周 -项目1 -验证算法(6)堆排序
- 算法第一次作业
- 【ZX笔试】短作业优先算法
- 吴恩达-DeepLearning.ai-Course1-Week2-实现逻辑回归算法-编程作业笔记
- 奇妙的算法—多作业多进程给定方案最大工作时间计算(修正版 机器阻塞版)
- 算法课作业2.7(判断并寻找主元素)
- 第十六周项目1-验证算法(3)冒泡排序
- 第十六周项目1(8)验证算法——基数排序