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 ...
随机推荐
- redis 拒绝远程访问解决
启动时报的警告: 1.Warning: no config file specified, using the default config. In order to specify a config ...
- System.Span, System.Memory,还有System.IO.Pipelines
System.Span, System.Memory,还有System.IO.Pipelines 使用高性能Pipelines构建.NET通讯程序 .NET Standard支持一组新的API,Sys ...
- Net Core2-JWT
NET Core2 http://www.cnblogs.com/wyt007/category/1130278.html JWT 设计解析及定制 前言 上一节我们讲述的书如何使用jwt token, ...
- docker jetty启动时报错 failed setting default capabilities.
docker 容器中jetty启动fail ,查看/var/log/jetty中的日志发现具体错误信息如下: failed setting default capabilities.set_caps( ...
- STM32之VCP1/VCAP2引脚的处理
需要根据仔细根据手册来决定这两个引脚是直接接地还是电容下拉到地 转载:STM32的Vcap的问题及解决---原来经验也害人http://bbs.eeworld.com.cn/thread-499497 ...
- github新手指南
- SpringMVC对HTTP报文体的处理
客户端和服务端HTTP报文传递消息,而HTTP报文包含报文头和报文体.通常,解析请求参数以及返回页面都不需要我们关心HTTP报文体的读取和生成过程.但在某些特定场景下需要直接到请求报文中读取报文体, ...
- 整理一些最近项目中用到的一些JS方法
验证是否为图片格式 function IsImgType(src) { var rFilter = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ ...
- C#里边的控件缩写大全(比较规范)
标准控件1 btn Button 2 chk CheckBox 3 ckl CheckedListBox 4 cmb ComboBox 5 dtp DateTimePicker 6 lbl Label ...
- tomcat调优方案Maximum number of threads (200) created for connector with address null and port 8091
1.tomcat6大并发出现:INFO: Maximum number of threads (200) created for connector with address null and por ...