120. Triangle(中等)
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(中等)的更多相关文章
- 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- ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode - 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode]题解(python):120 Triangle
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...
- leetcode 120 Triangle ----- java
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【LeetCode】120 - Triangle
原题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...
- 120. Triangle
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- LeetCode OJ 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- TCP/IP和HTTP协议代理
TCP/IP协议族 TCP/IP(传输控制协议/网际协议)是用于计算机通信的一个协议族. TCP/IP协议族包括诸如Internet协议(IP).地址解析协议(ARP).互联网控制信息协议(ICMP) ...
- 新概念英语(1-32)A fine day
新概念英语(1-33)A fine day Where is the Jones family? It is a fine day today. There are some clouds in th ...
- python入门(2)python的安装
python入门(2)python的安装 Python是跨平台的,可以运行在Windows.Mac和各种Linux/Unix系统上. 2.x还是3.x Python有两个版本,一个是2.x版,一个是3 ...
- io流的关闭顺序
1.一般先打开的后关闭,后打开的先关闭 2.可以只关闭处理流,因为io流使用了装饰模式,所以关闭处理流时,会调用节点流的close()方法.
- CWMP开源代码研究番外篇——博通方案
声明:本篇文章来自于某公司Cable Modem产品的文档资料,源码来自于博通公司,只提供参考(为保护产权,本人没有源码). 前文曾提到会写一篇关于博通的tr069,那么福利来了.福利,福利,福利,重 ...
- Java集合框架之四大接口、常用实现类
Java集合框架 <Java集合框架的四大接口> Collection:存储无序的.不唯一的数据:其下有List和Set两大接口. List:存储有序的.不唯一的数据: Set:存储无序的 ...
- STL源码剖析 — 空间配置器(allocator)
前言 以STL的实现角度而言,第一个需要介绍的就是空间配置器,因为整个STL的操作对象都存放在容器之中. 你完全可以实现一个直接向硬件存取空间的allocator. 下面介绍的是SGI STL提供的配 ...
- ML笔记:Deep Learning
非DL:要找好的特征 DL:无需找好的特征,但新问题:要设计好的网络架构
- [AtCoder agc021D]Reversed LCS
Description 题库链接 在改变原串 \(S\) 最多 \(K\) 个字母的前提下,使得 \(S\) 和 \(S\) 的反串的 \(LCS\) 尽量长,问最长长度. \(1\leq K\leq ...
- [Luogu 2062]分队问题
Description 给定n个选手,将他们分成若干只队伍.其中第i个选手要求自己所属的队伍的人数大等于a[i]人. 在满足所有选手的要求的前提下,最大化队伍的总数. 注:每个选手属于且仅属于一支队伍 ...