【LeetCode】triangle求最小和路径

今天闲来无事,做了三道leetcode水题,怒更两发博客,也挺不错。今天更新的两篇都是dp解法的简单题。


  • 题目要求如下。
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
[
     [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.
  • 题目说最好为O(n)的空间复杂度,想了想一般动规可能需要O(n2)的空间,如何可以只用O(n)呢,稍微脑筋一转就想到没有必要保存从根到每个节点的路径和啊!我们计算到第几行只需要保存当前行各节点的最短路径和,之前的我们根本不care,所以申请一个n的数组,每次都可以覆盖之前的。但要注意一个小地方,更新每一行的路径时,要从右侧开始,否则上一行的最短就会被覆盖掉了。

  • AC代码
class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        int n = triangle.size();
        if (n==0) return 0;
        if (n==1) return triangle[0][0];
        int dp[n+1];
        int Min = INT_MAX;
        for (int i=0;i<n+1;i++)
            dp[i]=INT_MAX;
        dp[1] = triangle[0][0];
        for (int i=1;i<n;i++){
            for (int j=i+1;j>0;j--){
                dp[j]=min(dp[j-1],dp[j])+triangle[i][j-1];
                if (i==n-1&&dp[j]<Min){
                    Min = dp[j];
                }
            }
        }
        return Min;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-08 18:56:46

【LeetCode】triangle求最小和路径的相关文章

LeetCode -- Triangle 路径求最小和( 动态规划问题)

人们常说"细节决定成败". 编码工作中,同样需要关注细节. 本文将给出3个小实例来说明编码中关注细节的重要性,同时给出作者对如何注意编码细节的一点见解(说的不对,请指正). 例1 这个问题如此地显而易见,竟然没有被发现. List<int> numList = new List<int>(); numList.Add(3); numList.Add(1); numList.Add(4); numList.Add(2); numList.Add(5); numLi

Leetcode:Triangle 三角形塔最小路径和

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 [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to botto

求最大边/最小边的比值最小的路径 codevs 1001 舒适的路线

codevs 1001 舒适的路线 2006年 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N(1<N≤500)个景点(编号为1,2,3,…,N),这些景点被M(0<M≤5000)条道路连接着,所有道路都是双向的,两个景点之间可能有多条道路.也许是为了保护该地的旅游资源,Z小镇有个奇怪的规定,就是对于一条给定的公路Ri,任何在该公路上行驶的车

求总和最小的路径

题目: 给定m*n的矩阵,每个位置是一个非负整数,从左上角开始,每次只能朝右和下走,走到右下角,但只走一次,求总和最小的路径. 思路: 因为只走一次,所以相对来说比较简单,dp[0, 0]=a[0, 0],且dp[x, y] = min(dp[x-1, y] + a[x, y],dp[x, y-1] + a[x, y]). #include <stdio.h> #include <stdlib.h> #define min(x, y) ((x)<(y)?(x):(y)) in

LeetCode: Triangle [120]

[题目] 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 [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 

HDU 3035 War(对偶图求最小割)

HDU 3035 War 题目链接 题意:根据图那样,给定一个网络,要求阻断s到t,需要炸边的最小代价 思路:显然的最小割,但是也显然的直接建图强行网络流会超时,这题要利用平面图求最小割的方法,把每一块当成一个点,共有边连边,然后每一个路径就是一个割,然后最短路就是最小割了 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace s

hdu 4862 KM算法 最小K路径覆盖的模型

http://acm.hdu.edu.cn/showproblem.php?pid=4862 选t<=k次,t条路要经过所有的点一次并且仅仅一次, 建图是问题: 我自己最初就把n*m 个点分别放入X集合以及Y集合,再求最优匹配,然后连样例都过不了,而且其实当时解释不了什么情况下不能得到结果,因为k此这个条件相当于没用上... 建图方法: 1.X集合和Y集合都放入n*m+k个点,X中前n*m个点和Y中前n*m个点之间,如果格子里的值相等,权就是(收益-耗费),不等就是(-耗费),因为要的是最大收益

HDU 4862 Jump(最小K路径覆盖)

输入一个n×m网格图,每个结点的值为0-9,可以从任意点出发不超过k次,走完每个点且仅访问每个结点一次,问最终的能量最大值.不可全部走完的情况输出-1. 初始能量为0. 而结点(x,y)可以跳跃到结点(x,y+dy)或(x+dx,y).消耗能量为跳跃前后结点的曼哈顿距离 - 1 .若跳跃前后的结点的值相等,能量加上那个值. 具体建图可以参考这里http://blog.sina.com.cn/s/blog_6bddecdc0102uy9g.html 最小K路径覆盖其实在之前是见过的打过的,不过这次

[luoguP2765] 魔术球问题(最大流—最小不相交路径覆盖)

传送门 枚举球的个数 num 如果 i < j && (i + j) 是完全平方数,那么 i -> j' 连一条边 再加一个超级源点 s,s -> i 再加一个超级汇点 t,i' -> t 那么当前可以放的柱子的最小数量就是最小不相交路径数 如果当前的最小不相交路径数 > num,break 求最大流的时候别忘了记录方案 ——代码 1 #include <cmath> 2 #include <queue> 3 #include <