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],
[2, 3],
[1,-1,-3],
]

结果应为 -1 + 2 + -1 = 0, 而不是 -1 + 2 + -3 = -2.

idea来自 http://www.cnblogs.com/liujinhong/p/5551932.html 中的图片,感谢这位同学.

本 code 属于 方法一: 自上而下,破坏原数组A. \(O(n^2)\) time, \(O(1)\) space

另一种方法 方法二: 自下而上,不破坏数组A. 维护一个长度为 n 的 1-dim 数组. \(O(n^2)\) time, \(O(n)\) space.

方法一的解题思路(请务必参照上面网址中的图片):

从上至下,将值按照"路径"加到下一层
除了A[0][0]这种情况外,还会遇到下列三种(注意判断条件)情况,共 4 cases:
case 1: left, right 上邻居都存在
case 2: left上不存在, right上存在
case 3: left上存在, right上不存在
case 4: A[0][0](它没left上 和 right上邻居), 我们 do nothing, 保留A[0][0]数值不变.

下面的图可以让我们看清上面 4 cases 的判断条件:

下面是元素索引:

	[
[0,0],
[1,0],[1,1] , [row-1,col-1],[row-1,col],
[2,0],[2,1],[2,2], [row, col]
[3,0],[3,1],[3,2],[3,3]
]

人家想法,自个代码(方法一,破坏原数组):

\(O(n^2)\) time, \(O(1)\) space

// idea来自 http://www.cnblogs.com/liujinhong/p/5551932.html
// 本 code 属于方法一:自上而下,破坏原数组A. $O(n^2)$ time, $O(1)$ space
// 另一种方法方法二:自下而上,不破坏数组A. 维护一个长度为 n 的 1-dim 数组.
// $O(n^2)$ time, $O(n)$ space.
int minimumTotal(vector<vector<int>>& A) {
const int n = A.size();
if (n == 0) return 0; // 从上至下,将值按照"路径"加到下一层
// 除了A[0][0]这种情况外,还会遇到下列三种情况,共 4 cases.
for (int row = 0; row < n; row++) {
for (int col = 0; col <= row; col++) {
if ((row - 1 >= 0) && (col - 1 >= 0) && (col <= row - 1)) {
// case 1: left, right 上邻居都存在
int mi = min(A[row - 1][col - 1], A[row - 1][col]);
A[row][col] += mi;
} else if ((row - 1 >= 0) && (col - 1 < 0) && (col <= row - 1)) {
// case 2: left上不存在, right上存在
A[row][col] += A[row - 1][col];
} else if ((row - 1 >= 0) && (col - 1 >= 0) && (col > row - 1)) {
// case 3: left上存在, right上不存在
A[row][col] += A[row - 1][col - 1];
}
// case 4: A[0][0](它没left上 和 right上邻居)
// do nothing, 保留A[0][0]数值不变.
}
} // 返回A中最下面的一行(A[n-1])最小元素
int res = INT_MAX;
for (int i = 0; i < A[n - 1].size(); i++) {
res = min(res, A[n - 1][i]);
}
return res;
}

方法二:

自下而上,不破坏数组A.

关键是找本层和上一层元素的关系,那就是 temp[j] = A[i,j] + min(temp[j], temp[j+1]).

\(O(n^2)\) time, \(O(n)\) space.

// 方法二:
// 自下而上,不破坏数组A. 维护一个长度为 n 的 1-dim 数组.
// $O(n^2)$ time, $O(n)$ space.
int minimumTotal(vector<vector<int>>& A) {
const int n = A.size();
if (n == 0)
return 0;
if (n == 1)
return A[0][0];
vector<int> temp;
// A 最后一行存入temp
for (int j = 0; j < n; j++)
temp.push_back(A[n - 1][j]); // 从倒数第二行到上按路径元素取min的,相加
// 对应关系:
// A[2, 1]
// temp[1] temp[1+1] for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
int smal = min(temp[j], temp[j + 1]);
// 若当前使用temp[0], temp[1]
// temp[0] 被改, 但不影响下次使用temp[1], temp[2]
temp[j] = A[i][j] + smal;
}
}
return temp[0];
}

120. Triangle(中等)的更多相关文章

  1. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

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

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

  3. LeetCode - 120. Triangle

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

  4. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

  5. leetcode 120 Triangle ----- java

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

  6. 【LeetCode】120 - Triangle

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

  7. 120. Triangle

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

  8. LeetCode OJ 120. Triangle

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

  9. LeetCode 120. Triangle (三角形)

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

随机推荐

  1. maven编译时出现读取XXX时出错invalid LOC header (bad signature)

    问题原因 该包没有下载正确. 解决办法 找到该包的目录,删除该包重新下载即可. 重新下载后用maven test一下,红叉消失.

  2. django的模型类管理器-----------数据库操作的封装

    模型实例方法 str():在将对象转换成字符串时会被调用. save():将模型对象保存到数据表中,ORM框架会转换成对应的insert或update语句. delete():将模型对象从数据表中删除 ...

  3. 在GridControl表格控件中实现多层级主从表数据的展示

    在一些应用场景中,我们需要实现多层级的数据表格显示,如常规的二级主从表数据展示,甚至也有多个层级展示的需求,那么我们如何通过DevExpress的GridControl控表格件实现这种业务需求呢?本篇 ...

  4. 框架学习之Struts2(四)---拦截器和标签

    一.拦截器概述 1.1 在struts2框架中封装了很多功能,struts2里面封装的功能都是在拦截器里面,struts2里面又很多拦截器,但不是每次这些拦截器都执行,每次执行型默认的拦截器. 默认拦 ...

  5. 《Java面向对象设计》

    <Java面向对象设计> 第一章 面向对象软件工程与UML p理解为什么需要软件工程 p掌握软件工程的基本概念 p掌握软件生命周期各个阶段的主要任务 p了解流行软件开发过程 p了解软件过程 ...

  6. background属性的学习整理转述

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! background我们一般用到的的属性有: background-attachment:背景(图片)是否 ...

  7. Python中的上下文管理器和with语句

    Python2.5之后引入了上下文管理器(context manager),算是Python的黑魔法之一,它用于规定某个对象的使用范围.本文是针对于该功能的思考总结. 为什么需要上下文管理器? 首先, ...

  8. iframe 里的高度适应的问题

    iframe 这个东西功能是很强大,但是有一个巨大的问题就是高度自适应的问题: 不过这个问题,百度或者谷歌上有很多解决办法,但是,很多时候都有兼容性问题: 所有我就每个方法都试了一遍,终于找到了一个 ...

  9. [LeetCode] 01 Matrix 零一矩阵

    Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance b ...

  10. IOS开发- 访问通讯录,并将通讯录中姓名-头像-手机号码 发给服务器

    现在很多软件都会访问通讯录,并将通讯录的信息取得,发给服务器,然后服务器会返回相应电话号码的用户是否注册. 现在分享一下前两步,访问通讯录并处理通讯录的信息 1.导入框架 #import <Ad ...