Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.(Medium)

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.

分析:

经典的动态规划题,可以有自顶向下,自底向上的解法,是自己学动态规划的第一道题,就不分析了,写一个自底向上的解法。

代码:

 class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int sz = triangle.size();
if (sz == ) {
return ;
}
int dp[sz][sz];
for (int i = ; i < sz; ++i) {
dp[sz - ][i] = triangle[sz - ][i];
}
for (int i = sz - ; i >= ; --i) {
for (int j = ; j <= i; ++j) {
dp[i][j] = min(dp[i + ][j], dp[i + ][j + ]) + triangle[i][j];
}
}
return dp[][];
}
};

LeetCode120 Triangle的更多相关文章

  1. LeetCode120——Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  2. Leetcode120.Triangle三角形最小路径和

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

  3. [Swift]LeetCode120. 三角形最小路径和 | Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. [LeetCode] Triangle 三角形

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  6. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  7. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  8. 【leetcode】Pascal's Triangle

    题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. 【html、CSS、javascript-2】CSS基础

    CSS CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一 css的四种引入方式 1.行内式          ...

  2. JavaScript内容梳理 示例之模态对话框 示例之全选和反选以及取消 示例之后台管理左侧菜单

    <!DOCTYPE html> <!--示例之模态对话框--> <html lang="en"> <head> <meta c ...

  3. TZ_13_负载均衡-Robbin

    1.但是实际环境中,我们往往会开启很多个user-service的集群.此时我们获取的服务列表中就会有多个,到底该访问哪一个呢? 一般这种情况下我们就需要编写负载均衡算法,在多个实例列表中进行选择. ...

  4. android 读取.properties文件

    因为最终是通过流文件来进行properties文件读取的,所以很自然,我们想到要将文件放入到assets文件夹或者raw文件夹中了. 例如,我们这里有一个文件——>test.properties ...

  5. golang redis_example

    main.go package main import ( "fmt" "github.com/garyburd/redigo/redis" ) func ma ...

  6. beego应用做纯API后端如何使用jwt实现无状态权限验证

    jwt是什么,可以百度下其它文章,我原来看到一个讲的详细的,现在找不到了.先简单介绍下我个人的理解,就是一个token,只不过通过加密解密的手段,能让这一串字符带有一些简单的信息.这样解密jwt后不用 ...

  7. C++函数部分总结

    目录 为什么要使用函数 为什么要用函数重载 C++传参方式 特殊的函数--递归函数 为什么要使用函数 使用函数可以将一个比较复杂的程序系统的分为若干块简洁的模块,使程序更加清晰明了 比如,我们想要模拟 ...

  8. DOM 创建元素 删除元素(结点)

    创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一个已存在的元素追加该元素. <script> var para=document. ...

  9. python 中if __name__=="__main__"

    if __name__=="__main__":表示当执行运行文件为当前代码所在文件时,则会执行if__name__=="__main__":后的语句. 如果这 ...

  10. excel怎么并排查看两个工作表

    excel怎么并排查看两个工作表 excel怎么并排查看两个工作表?excel打开一个窗口想要同时查看两个工作表中的数据,类似于word中的分栏效果,该怎么实现呢?EXCEL是一个使用最多的办公软件, ...