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层的数到底层的最小和时,如果我们知道第i+1层的数到底层最小的和就好算了。即minsum[i][j]=triangle[i]+min( minsum[i+1][j] , minsum[i+1][j+1] );从底层向顶层逐层计算,就能得到最终结果。

本文使用大小为n的数组d记录每一层的结果,达到了O(n)的空间复杂度要求。

class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int s = triangle.size();
if(s != (triangle[s-1].size()))
return -1;
if(s==1)
return triangle[0][0];
int *d = new int[s];
int i,j;
for(i=0;i<s;i++)
d[i]=triangle[s-1][i];
for(i=s-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
d[j]=triangle[i][j]+min(d[j],d[j+1]);
}
}
return d[0];
}
};

Triangle leetcode的更多相关文章

  1. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  2. Triangle——LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. Pascal's Triangle leetcode

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. Triangle LeetCode |My solution

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. Pascal's Triangle leetcode java(杨辉三角)

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  6. Triangle leetcode java

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  7. triangle leetcode C++

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  9. LeetCode第二天&第三天

    leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...

随机推荐

  1. sql 中的运算符级别 如and or not

    写了这么多简单的sql,很多东西忘记得差不多了,差点连最基本sql运算符优先级都忘了.平时最常用到and or的优先级都忘了 and的优先级高于or的优先级 举个例子 select * from us ...

  2. char *p = "abcdefg"; p[0] = p[1]出错

    参考:http://blog.sina.com.cn/s/blog_5c0172280100ut4o.html 1.char *s="abc"; 看这个赋值: 右边,是" ...

  3. 使用js进行string和json之间转换的方法

    在数据传输过种中,json是以文本,即字符串的形式传递,字符串形似Json对象: var str1 = '{ "name": "Amy", "sex& ...

  4. JavaWeb学习笔记——jsp:setproperty和getproperty

  5. 关于软件工程结对编程作业 PairProject : Elevator Scheduler(电梯调度算法的实现与测试)的总结

    1)结对编程队友 1106xxxx 张扬 1106xxxx 杨军 其中,此项目的编程实现主要由前者完成. 2)关于结对编程 结对编程的优点: 最直接的一点:在结对编程中,由于有另一个人在你身边和你配合 ...

  6. Windows 无法自动将 IP 协议堆栈绑定到网络适配器。解

    Windows 无法自动将 IP 协议堆栈绑定到网络适配器.解  昨天断网了,所以把珍藏已久的无线网卡拿出来蹭网.我系统是Windows 7 但是装上去东显示已启用,就是用不了,用windows诊断是 ...

  7. winSocket数据传输

    服务器端: #include <WINSOCK2.H> #include <stdio.h> #pragma comment(lib,"ws2_32.lib" ...

  8. javascript 小技巧

    1:Boolean()==!! console.log(Boolean(888));//true console.log(!!(888));//true console.log(Boolean(&qu ...

  9. Yii2-admin RBAC权限管理的实现

    原文地址:http://www.open-open.com/lib/view/open1434638805348.html   http://wlzyan.blog.163.com/blog/stat ...

  10. Web启动服务器上的某一个服务

    情景是这样的.. 网页打开一个数据列表..数据要求实时从其他多个平台上获取.. 所以就有了一个Web页面..还有个WinService的服务来定时获取这些数据... 问题来了.. 发现Service有 ...