题目

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;
}
};

GitHub测试程序源码

LeetCode(120) Triangle的更多相关文章

  1. LeetCode(120):三角形最小路径和

    Medium! 题目描述: 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] ...

  2. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. 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 ...

  6. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 《springcloud 二》微服务动态网关,网关集群

    动态网关    实际上是网关和分布式配置中心的整合,通过post手动刷新,生效 动态网关 传统方式将路由规则配置在配置文件中,如果路由规则发生了改变,需要重启服务器.结合整合SpringCloud C ...

  2. nio aio netty区别

    传统io就是bio     同步阻塞         但可以采用伪同步 nio  jdk1.7以前     同步非阻塞io     1.7以后     同步异步非阻塞                  ...

  3. c#的Lambda 表达式

    首先看官方的说法: Lambda 表达式是一种可用于创建委托或表达式目录树类型的匿名函数. 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数. Lambda 表达式 ...

  4. Tomcat一

    Tomcat是如何处理http请求的 Tomcat有什么用? Tomcat是一个应用服务器,也是一个Servlet容器,用来接收前端传过来的请求,并将请求传给Servlet,并将Servlet的响应返 ...

  5. java初探之初始化

    首先明确,变量初始化是在任何方法(包括构造器)被调用之前进行的. 1.实例变量的初始化 实例变量只有当它所属的类实例化后才会存在,构造器被执行就意味着对象就被创建. 1.1.指定初始化. class ...

  6. SpringBoot热部署的两种方式

    SpringBoot热部署方式一共有两种,分别使用两种不同的依赖 SpringBoot 1.3后才拥有SpringBoot devtools热部署 ①:spring-boot-devtools   ② ...

  7. AngularJS所有版本下载地址

    AngularJS官网本身采用AngularJS库构建,页面中的AngularJS库通过Google的CDN(内容分发网络)引入,所以国内访问会有问题. 大家可以从下面地址获取AngularJS所以版 ...

  8. C++ string 类型提取字符串

    在某些情况下需要对输入的字符串进行处理,提取其中的需要的信息. 比如在linux中输入"mkdir test",新建test文件夹,就需要提取其中的test字符. 提取的方法需要b ...

  9. Linux之bash shell的学习

    1.什么是bash  shell bash 是Bourne Again Shell的简称,是从unix系统中的sh发展而来,是用户和偶Linux内核交互的工具,用户通过bash操作内核完成系统的使用和 ...

  10. python爬虫之路——初识数据库存储

    非关系型数据库:MongoDB.关系型数据库:MySQL 关系型和非关系型的区别: 安装: 使用: 应用场景: mongoDB是一种非关系型数据库,分为四大类:键值存储数据库,列存储数据库,文档型数据 ...