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. 动态规划: 到第i层的第k个顶点的最小路径长度表示为f(i,j),则:

f(i,j) = min{f(i,j + 1),f(i + 1, j + 1)}+ (i,j)

  1. 本题主要关心的是空间复杂度不要超过n。
  2. 注意边界条件——每一行中的第一和最后一个元素在上一行中只有一个邻居。而其他中间的元素在上一行中都有两个相邻元素。

算法实现代码:

class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int len = triangle.size();
for (int i = len- 2; i >= 0; i--)
for (int j = 0; j < i + 1; ++j){
if(triangle[i+1][j] > triangle[i+1][j+1]){
triangle[i][j] += triangle[i+1][j+1];
}
else{
triangle[i][j] += triangle[i+1][j];
}
}
return triangle[0][0];
}
};

  

【leetcode】Triangle (#120)的更多相关文章

  1. 【LeetCode】Triangle 解决报告

    [称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

  2. 【leetcode】triangle(easy)

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

  3. 【Leetcode】Triangle

    给定一个由数字组成的三角形,从顶至底找出路径最小和. Given a triangle, find the minimum path sum from top to bottom. Each step ...

  4. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  7. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. 【BZOJ 4561】【JLOI 2016】圆的异或并

    http://www.lydsy.com/JudgeOnline/problem.php?id=4561 一开始并不会做,后来看题解看懂了. 看懂了之后还是错了好几次,数组大小手残开小了. 圆的包含并 ...

  2. ES6新特性:使用新方法定义javascript的Class

    ES6中定义类的方式, 就是ES3和ES5中定义类的语法糖,虽然也有些区别,但是整体定义类的方式更加简洁,类的继承更加方便, 如果想对ES6中的继承更加熟悉, 最好了解ES5中原型继承的方式, 博客园 ...

  3. HTML5图形图像处理技术研究

    摘要:图形图像处理平台大部分是传统的C/S架构的桌面应用程序,维护困难,共享性差,而B/S架构的Web程序具有易维护.易共享的优点.本文研究了基于HTML5的Web图形图像处理技术,用HTML5实现了 ...

  4. Zabbix监控nginx-rtmp status(html版)

    nginx-rtmp开启stats # nginx(--add-module=nginx-rtmp-module-master) nginx.conf: server { listen ; locat ...

  5. <<< 三大框架简短介绍

    Struts 搞业务 Spring 主要是AOP(面向方面编程)和IOC(控制反转)它里面用到很多的设计模式Hibernate Orm映射工具 实现面向对象的方式操作数据库hibernate 封装了j ...

  6. 转 PresentViewController切换界面

    视图切换,没有NavigationController的情况下,一般会使用presentViewController来切换视图并携带切换时的动画, 其中切换方法如下: – presentViewCon ...

  7. Google Map API V3开发(1)

    Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...

  8. Canvas绘制渐变

    1.绘制线性渐变 Canvas提供了用于创建线性渐变的函数createLinearGradient(x0,y0,x1,y1),坐标点(x0,y0)是起点 ,(x1,y1)是终点 创建一个渐变色 var ...

  9. [NHibernate]代码生成器的使用

    目录 写在前面 文档与系列文章 代码生成器的使用 总结 写在前面 前面的文章介绍了nhibernate的相关知识,都是自己手敲的代码,有时候显得特别的麻烦,比如你必须编写持久化类,映射文件等等,举得例 ...

  10. 开始学红帽的RHCE课堂有2次课了,要记下自己的学习经历

    我终于申请成功了博客园的博客了. 红帽课堂已经开始2次了,这里的记录可能不分顺序,每天记录一点自己的学习内容.方便自己以后查询. 已经学了以下内容: 1.访问命令行 使用桌面的访问命令 GNOME 3 ...