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. OAuth2.0学习(1-4)授权方式1-授权码模式(authorization code)

    参与者列表: (1) Third-party application:第三方应用程序,又称客户端(client),如:"云冲印".社交应用. (2)HTTP service:HTT ...

  2. 框架学习笔记之Mybatis(二)

    一.动态sql 通过mybatis提供的标签,实现sql语句的拼接. 1.where <select id="findUserList" parameterType=&quo ...

  3. POJ-1182 食物链---并查集(附模板)

    题目链接: https://vjudge.net/problem/POJ-1182 题目大意: 中文题,不多说. 思路: 给每个动物创建3个元素,i-A, i-B, i-C i-x表示i属于种类x,并 ...

  4. requests+正则爬取豆瓣图书

    #requests+正则爬取豆瓣图书 import requests import re def get_html(url): headers = {'User-Agent':'Mozilla/5.0 ...

  5. Java进阶篇(一)——接口、继承与多态

    前几篇是Java的入门篇,主要是了解一下Java语言的相关知识,从本篇开始是Java的进阶篇,这部分内容可以帮助大家用Java开发一些小型应用程序,或者一些小游戏等等. 本篇的主题是接口.继承与多态, ...

  6. [ZJOI 2015]幻想乡战略游戏

    Description 傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网游厂商把游戏的地图越做越大,以至于幽香一眼根本看不过来, ...

  7. [NOIp 2017]列队

    Description Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有$n \times m$名学生, ...

  8. bzoj 1272: [BeiJingWc2008]Gate Of Babylon

    Description Solution 如果没有限制,答案就是 \(\sum_{i=0}^{m}C(n+i-1,i)\) 表示枚举每一次取的个数,且不超过 \(m\),方案数为可重组合 发现这个东西 ...

  9. [ZJOI2006]物流运输 SPFA+DP

    题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪. ...

  10. 【LSGDOJ 1852】青蛙的烦恼 DP

    题目描述 池塘中有n片荷叶恰好围成了一个凸多边形,有一只小青蛙恰好站在1号荷叶上,小青蛙想通过最短的路程遍历所有的荷叶(经过一个荷叶一次且仅一次),小青蛙可以从一片荷叶上跳到另外任意一片荷叶上. 输入 ...