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. the pipeline of call SNP

    ######################################## ############### Mapping ################ ################## ...

  2. java高新技术-java5的静态导入与编译器语法设置

    静态导入 import语句可以导入一个类或某个包中的所有类 import static 语句导入有一个类中的某个静态方法或所有静态方法 使用Math.random() 可以这样做 package co ...

  3. 【BZOJ-3643】Phi的反函数 数论 + 搜索

    3643: Phi的反函数 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 141  Solved: 96[Submit][Status][Discuss ...

  4. 最简单的JavaScript模板引擎

    在小公司待久了感觉自己的知识面很小,最近逛博客园和一些技术网站看大家在说JavaScript模版引擎的事儿,完全没有概念,网上一搜这是08年开始流行起来的...本来以为这是很高深的知识,后来在网上看到 ...

  5. oracle中将自建用户下的所有表删除

    select 'drop table '||table_name||' ;' from user_tables;select 'drop sequence '||sequence_name||' ;' ...

  6. linux 遇见的问题

    Permissions 0644 for '/root/.ssh/id_rsa' are too open.问题 如果出现 Permissions 0644 for '/root/.ssh/id_rs ...

  7. 12.3 Arithmetic-software-

    一.流程图: 二.软件功能: 分为混合模式和单则模式,混合模式生成指定题数的任意四则混合运算,数字是1-10之间随机:单则模式是生成指定题数的加减乘除中指定的某一则运算,数字是1-10之间随机. 用户 ...

  8. jvm垃圾回收机制

    http://blog.csdn.net/zsuguangh/article/details/6429592 原文地址

  9. java -日期处理

    1. 计算某年某月份 总有多少个周,每周的开始和结束时间? 思路:1.计算出本月实际的总天数 2.循环每一天,判断这天是否是 周日(1),如果是,周数加1,再次判断是否是月的第一个周一,如是,开始时间 ...

  10. css3 动画效果 定义和绑定执行

    首先要定义一个动画效果  keyframes 关键字 这里动画效果执行完毕后 恢复本身的css样式  有的动画效果 移动到位置 要保持 就需要写好css 元素的位置 css里直接写  (这里是一般的 ...