LeetCode 120. Triangle三角形最小路径和 (C++)
题目:
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.
分析:
给定一个三角形,找出自顶向下的最小路径和。每一步只能移动到下一行中相邻的结点上。
从最底层往上去找,开辟一个二维数组res,其中res[i][j]记录到达该点时,最小的路径和,状态转移方程是res[i][j] = min(res[i+1][j]+triangle[i][j], res[i+1][j+1]+triangle[i][j]),也就是到达这个位置的最小路径和,等于当前的值加上下一层相邻的最小路径和的较小的值,值得注意的是,数组的形式实际是如下的:
[2],
[3,4],
[6,5,7],
[4,1,8,3]
而不是看起来那样的三角形,注意索引就好。
题中说使用 O(n) 的额外空间,一旦理解了这道题的思路,就很容易做了,开辟一个大小为三角形行数大小的数组,每一次状态更新时,在数组中更新就好。
res[j] = min(res[j]+triangle[i][j], res[j+1]+triangle[i][j]);
程序:
//O(n^2) space
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int m = triangle.size();
vector<vector<int>> res(m+, vector<int>(m+, ));
//res[i][j] = min(res[i+1][j]+triangle[i][j], res[i+1][j+1]+triangle[i][j])
for(int i = m-; i >= ; --i)
for(int j = ; j < triangle[i].size(); ++j)
res[i][j] = min(res[i+][j]+triangle[i][j], res[i+][j+]+triangle[i][j]);
return res[][];
}
};
//O(n) space
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int m = triangle.size();
vector<int> res(m+, );
//res[j] = min(res[j]+triangle[i][j], res[j+1]+triangle[i][j])
for(int i = m-; i >= ; --i)
for(int j = ; j < triangle[i].size(); ++j)
res[j] = min(res[j]+triangle[i][j], res[j+]+triangle[i][j]);
return res[];
}
};
LeetCode 120. Triangle三角形最小路径和 (C++)的更多相关文章
- LeetCode 120. Triangle (三角形最小路径和)详解
题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...
- 120 Triangle 三角形最小路径和
给出一个三角形(数据数组),找出从上往下的最小路径和.每一步只能移动到下一行中的相邻结点上.比如,给你如下三角形:[ [2], [3,4], [6,5,7], [4,1,8,3]] ...
- [算法]LeetCode 120:三角形最小路径和
题目描述: 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3]]自顶向下的最小路径和 ...
- LeetCode刷题: 【120】三角形最小路径和
1. 题目: 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小 ...
- Leetcode120.Triangle三角形最小路径和
给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径和为 11 ...
- Java实现 LeetCode 120 三角形最小路径和
120. 三角形最小路径和 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] ...
- 领扣-120 三角形最小路径和 Triangle MD
三角形最小路径和 Triangle 数组 动态规划 问题 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [2], [3,4], [6,5,7], ...
- leetcode 120. 三角形最小路径和 及 53. 最大子序和
三角形最小路径和 问题描述 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] ...
- 1. 线性DP 120. 三角形最小路径和
经典问题: 120. 三角形最小路径和 https://leetcode-cn.com/problems/triangle/ func minimumTotal(triangle [][]int) ...
随机推荐
- Haproxy+Keepalived构建高可用负载均衡集群
实验环境: 主机名 IP地址 VIP 192.168.200.254 Haproxy-1 192.168.200.101 Haproxy-2 192.168.200.102 Nginx1 192.16 ...
- Docker入门之安装与使用
1. 安装(windows) win7.win8以及win10家庭版 等需要利用 docker toolbox 来安装,国内可以使用阿里云的镜像来下载,下载地址:http://mirrors.aliy ...
- 【linux】查看GPU使用率
nvidia-smi -l 1 每秒刷新一次
- LeetCode 20:有效的括号 Valid Parentheses
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. Given a string containing just the characters '(', ' ...
- JeeSite | 数据权限应用
中午吃饭时看了一下陆毅版的<三国>,刚好看的是蜀军缺粮,诸葛亮让王平去劫司马懿的粮.司马懿看蜀军用木牛流马运量很方便,就抢了蜀军的木牛流马仿制了一批,结果司马懿用它运粮时,被王平冒充司马懿 ...
- re2c安装
wget https://kojipkgs.fedoraproject.org//packages/re2c/1.1.1/3.fc31/src/re2c-1.1.1-3.fc31.src.rpm 解 ...
- 打开IDEA的更新选项,如何打开IDEA更新弹窗
如何让IDEA的更新弹窗重新出现,打开IDEA的更新选项 IDEA update的时候,会提示一个更新的弹框选择框如下图所示 在最下方有个Do not show this dialog in the ...
- python实现词云
一.安装使用命令[pip install wordcloud]安装词云 二.参数使用了OpenCV的数据格式进行读取,字体可以多试几种 def create_wordcloud_pic(): stop ...
- WPF-控件模板
说起控件模板,还是因为在一次需求实现中,我碰到了一个圆形按钮.一开始我认知的按钮是方形灰不拉几的一个块儿.这如何实现一个圆形按钮? 我最先想到的是使用样式,可是发现根本就没有改变Button形状的属性 ...
- GDAL读取Shapefile
-------------------------------------------------------------------------------------- #include < ...