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

  1. [
  2. [2],
  3. [3,4],
  4. [6,5,7],
  5. [4,1,8,3]
  6. ]

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.

这道题和Dungeon Game 地牢游戏非常的类似,都是用动态规划Dynamic Programming来求解的问题。而且递推式也比较容易看出来,我最先想到的方法是:

从第二行开始,triangle[i][j] = min(triangle[i - 1][j - 1], triangle[i - 1][j]), 然后两边的数字直接赋值上一行的边界值,由于限制了空间复杂度,所以我干脆直接就更新triangle数组,代码如下:

  1. class Solution {
  2. public:
  3. int minimumTotal(vector<vector<int> > &triangle) {
  4. int n = triangle.size();
  5. for (int i = ; i < n; ++i) {
  6. for (int j = ; j < triangle[i].size(); ++j) {
  7. if (j == ) triangle[i][j] += triangle[i - ][j];
  8. else if (j == triangle[i].size() - ) triangle[i][j] += triangle[i - ][j - ];
  9. else {
  10. triangle[i][j] += min(triangle[i - ][j - ], triangle[i - ][j]);
  11. }
  12. }
  13. }
  14. int res = triangle[n - ][];
  15. for (int i = ; i < triangle[n - ].size(); ++i) {
  16. res = min(res, triangle[n - ][i]);
  17. }
  18. return res;
  19. }
  20. };

这种方法可以通过OJ,但是毕竟修改了原始数组triangle,并不是很理想的方法。在网上搜到一种更好的DP方法,这种方法复制了三角形最后一行,作为用来更新的一位数组。然后逐个遍历这个DP数组,对于每个数字,和它之后的元素比较选择较小的再加上上面一行相邻位置的元素做为新的元素,然后一层一层的向上扫描,整个过程和冒泡排序的原理差不多,最后最小的元素都冒到前面,第一个元素即为所求。代码如下:

  1. class Solution {
  2. public:
  3. int minimumTotal(vector<vector<int> > &triangle) {
  4. int n=triangle.size();
  5. vector<int> dp(triangle.back());
  6. for(int i=n-;i>=;i--){
  7. for(int j=;j<triangle[i].size();j++){
  8. dp[j]=min(dp[j],dp[j+])+triangle[i][j];
  9. }
  10. }
  11. return dp[];
  12. }
  13. };

下面我们来看一个例子,对于输入数组:

-1

2   3

1  -1  -3

5   3   -1   2

下面我们来看DP数组的变换过程。

DP:5  3  -1  2

DP:4  3  -1  2  :1+min(5,3)=4

DP:4  -2  -1  2 :-1+min(3,-1)=-2

DP:4  -2  -4  2 :-3+min(-1,2)=-4

DP:0  -2  -4  2 :2+min(4,-2)=0

DP:0  -1  -4  2 :3+min(-2,-4)=-1

DP:-2  -1  -4  2 :-1+min(0,-1)=-2

Triangle 三角形——找出三角形中从上至下和最小的路的更多相关文章

  1. 找出数组中从未出现的最小正整数java实现

    /** * 找出未出现的最小正整数 * @param A * @param n * @date 2016-10-7 * @author shaobn */ public static int find ...

  2. [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  3. 剑指Offer 找出字符串中第一个只出现一次的字符

    题目描述 找出字符串中第一个只出现一次的字符 如果无此字符 请输出'.' 输入描述: 输入一串字符,由小写字母组成 输出描述: 输出一个字符 输入例子: asdfasdfo 输出例子: o 思路:数组 ...

  4. 找出字符串中第一个不重复的字符(JavaScript实现)

    如题~ 此算法仅供参考,小菜基本不懂高深的算法,只能用最朴实的思想去表达. //找出字符串中第一个不重复的字符 // firstUniqueChar("vdctdvc"); --& ...

  5. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  6. 找出数组中出现奇数次的元素<异或的应用>

    点击打开链接:百度面试题之找出数组中之出现一次的两个数(异或的巧妙应用) 题目描述|:给定一个包含n个整数的数组a,其中只有一个整数出现奇数次,其他整数都出现偶数次,请找出这个整数 使用异或操作,因为 ...

  7. 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数

    找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...

  8. 找出数组中最大值and索引

    找出数组中的最大值和和最大值的索引位置..... 第一中方法: /** * 找出数组中最大值和最大值的索引 * @param args */ public static void main(Strin ...

  9. 剑指offer:1.找出数组中重复的数(java版)

    数组中重复的数:题目:找出数组中重复的数,题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任 ...

随机推荐

  1. C语言变量长度在32位和64位处理器上的关系

    C语言变量长度在32位和64位处理器上的关系       理论上来讲 我觉得数据类型的字节数应该是由CPU决定的,但是实际上主要由编译器决定(占多少位由编译器在编译期间说了算).常用数据类型对应字节数 ...

  2. git status 下中文显示乱码问题解决

      $ git status -s                 ?? "\350\257\264\346\230\216.txt\n                 $ printf & ...

  3. cf- 297 < a >--字符串操作技巧

    A. Vitaliy and Pie time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. 广东工业大学2016校赛决赛重现——E积木积水(方法据说很多)

    Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...

  5. LibreOJ2045 - 「CQOI2016」密钥破解

    Portal Description 给出三个正整数\(e,N,c(\leq2^{62})\).已知\(N\)能表示成\(p\cdot q\)的形式,其中\(p,q\)为质数.计算\(r=(p-1)( ...

  6. UVA12230 Crossing Rivers (数学期望)

    题目链接 题意翻译 一个人每天需要从家去往公司,然后家与公司的道路是条直线,长度为 \(D\). 同时路上有 \(N\) 条河,给出起点和宽度\(W_i\) , 过河需要乘坐速度为\(V_i\) 的渡 ...

  7. 【CF712D】Memory and Scores(概率,DP,前缀和)

    题意:AB两人玩一个游戏,两人玩t轮 每人每次随机且等概率从[-k,k]中取一个数字加到总得分中 得分高者赢 已知A B初始分别有a b分,问A取得胜利的概率是多少 (1 ≤ a, b ≤ 100,  ...

  8. Method and apparatus for verification of coherence for shared cache components in a system verification environment

    A method and apparatus for verification of coherence for shared cache components in a system verific ...

  9. Linux 之 文件搜索命令

    文件搜索命令 参考教程:[千峰教育] 文件搜索定位 grep: 作用:通用规则表达式分析程序,是一种强大的文本搜索工具, 它能使用正则表达式搜索文本,并把匹配的行打印出来. 格式:grep [选项] ...

  10. CSS-实现倒影效果box-reflect

    我需要的效果: html: <img src="images/my1.jpg" width="20%"/> css:   img{-webkit-b ...