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 ...
随机推荐
- YML(1)什么是 YML
YAML(IPA: /ˈjæməl/,尾音类似camel骆驼) YAML 是一个可读性高,用来表达资料序列的编程语言. YAML参考了其他多种语言,包括:XML.C语言.Python.Perl以及电子 ...
- 我的jquery validate 笔记
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF- ...
- python flask框架 数据库的使用
#coding:utf8 from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) # ...
- Android 自定义控件高度设置onMeasure方法
最近使用hellocharts需要表格横向显示,而activity需要竖屏显示,在旋转以后,默认宽度为不超过屏幕宽度,则一直无法显示全控件. 此时需要修改onMeasure方法,这个方法是用来控制控件 ...
- Mysql 5.1的坑
1.数据库表是区分大小写的 之前程序在5.7数据库没问题,测试环境上数据库是5.1的,就提示表找不到. 2.同样的sql,在5.1上会提示事务获取锁失败,超时返回.而5.7上正常.原因暂未找到.
- 1.5 WEB API 上传文件
1.前提,设置跨域 2.在控制器头添加允许跨域 /// <summary> /// 文件管理口控制器 /// </summary> [EnableCors("*&qu ...
- SQL基础----DCL
在之前的文章已经讲到SQL基础DDL(数据库定义语句 http://www.cnblogs.com/cxq0017/p/6433938.html)和 DML(数据库操作语句 http://www.cn ...
- WebGL文字渲染的那些问题
THREE.js开发的应用运行在iphone5下发现有些时候会崩溃,跟了几天发现是因为Sprite太多频繁更新纹理占用显存导致的.通常解决纹理频繁更新问题就要用到one draw all方法,放到纹理 ...
- 【js 笔记】读阮一峰老师 es6 入门笔记 —— 第一章
鉴于最近用 vuejs 框架开发项目,其中有很多涉及到 es6 语法不太理解所以便认真地读了一下这本书. 地址:http://es6.ruanyifeng.com/#README 第一章:let ...
- [LeetCode] Array Partition I 数组分割之一
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...