LeetCode(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
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
分析
本题类似于之前的一个障碍物的题目,用到动态规划的思想;
分析第i层的第k个顶点的最小路径长度表示为
f(i,k),则f(i,k)=minf(i−1,k),f(i−1,k−1)+d(i,k); (注意每行的首尾边界需要特殊处理)
其中d(i,k)表示原来三角形数组里的第i行第k列的元素。
则可以求得从第一行到最终到第rows−1行第k个元素的最小路径长度,最后再比较第rows−1行中所有元素的路径长度大小,求得最小值。
题目要求:空间复杂度不要超过n。
AC代码
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
if (triangle.empty())
return 0;
int rows = triangle.size();
//动态规划,由于空间复杂度要求,现利用原始二维数组triangle改为存储当前(i,j)位置的最小和
for (int i = 1; i < rows; ++i)
{
int cols = triangle[i].size();
for (int j = 0; j < cols; ++j)
{
//本行的第一个元素
if (0 == j)
{
triangle[i][j] = triangle[i][j] + triangle[i - 1][j];
}
//本行的最后一个元素
else if (j == cols - 1)
{
triangle[i][j] += triangle[i - 1][j - 1];
}
else{
triangle[i][j] = min(triangle[i][j] + triangle[i][j - 1], triangle[i][j] + triangle[i - 1][j - 1]);
}//else
}//for
}//for
//最小路径和为最后一行的最小值
int minSum = triangle[rows - 1][0];
for (int j = 0; j < triangle[rows - 1].size(); ++j)
{
if (minSum > triangle[rows - 1][j])
minSum = triangle[rows - 1][j];
}//for
return minSum;
}
};
LeetCode(120) Triangle的更多相关文章
- LeetCode(120):三角形最小路径和
Medium! 题目描述: 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- 使用xadmin覆盖Django的admin
安装xadmin pip 安装 pip install xadmin 可能会报错 pip install git+git://github.com/sshwsfc/xadmin.git 安装后要使用 ...
- (转)useradd用户,组管理案例
原文:https://www.cnblogs.com/Csir/p/6403830.html?utm_source=itdadao&utm_medium=referral 示例:要求oldbo ...
- MapReduce基本流程与设计思想初步
1.MapReduce是什么? MapReduce是一种编程模型,用于大规模数据集的并行运算.它借用了函数式的编程概念,是Google发明的一种数据处理模型. 主要思想为:Map(映射)和Reduce ...
- 获取spring里的bean
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring. ...
- 在linux上怎么查看tomcat日志
进入到tomcat的logs文件夹 tail -f catalina.out
- Tensorflow版Faster RCNN源码解析(TFFRCNN) (2)推断(测试)过程不使用RPN时代码运行流程
本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第二篇 推断(测试)过程不使用RPN时代码运行流程 作者:Jiang Wu 原文见:https://hom ...
- IOC的使用
1.概要: 在spring中,都是使用反射机制创建对象,将创建对象的权利交给spring容器,就是控制反转(ioc) 对象创建方式 有参构造 无参构造 工厂模式(静态,非静态) 2.创建IDEA控制台 ...
- canvas雪花特效-jQuery插件实现
这是一款效果十分逼真的html5 canvas下雪场景动画特效插件.这款下雪特效是基于Jason Brown的Snowfall jquery plugin的基础上制作的.在Snowfall jquer ...
- 微软打造了全球最大的Git代码库
丹棱君有话说:今年 2 月,微软宣布将用 Git 管理 Windows 源代码.随后,Visual Studio 宣布开发 “Git 虚拟文件系统(GVFS)”,并将在终极项目和超大型团队中推行 Gi ...
- 洛谷 P2424 约数和
题目背景 Smart最近沉迷于对约数的研究中. 题目描述 对于一个数X,函数f(X)表示X所有约数的和.例如:f(6)=1+2+3+6=12.对于一个X,Smart可以很快的算出f(X).现在的问题是 ...