Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

还是DP问题,给定一个m*n的二维数组,每一个值都是非负数。问从起点(左上角)到终点(右下角)的一条路径上所有的数加起来的和最小是多少?

解题思路:

参考  Unique Path Two题目的解法  在那个基础上:

以中间的某一个点A来考虑,A左边是B,A上边是C,假设B和C的结果已经求出来了,那么A的结果应该等于B和C中结果较小的那个加上A位置的给定值。

特殊的是:

第一行中,除第一个点的所有的点的结果等于前一个点的结果加上本身的给定值。

第一列中,除第一个点的所有的点的结果等于上一个点的结果加上本身的给定值。

(描述起来好绕口的说…………)

画图来理解:

代码如下:

空间复杂度:O(n)

class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int m = grid.size();
int n = grid[].size();
vector<int> dp(n,);
for(int i = ; i < m;i++){
for(int j = ; j < n; j++){
if(j == ){
dp[j] = dp[j] + grid[i][j];
}
else if(i == ){
dp[j] = dp[j-] + grid[i][j];
}
else{
dp[j] = min(dp[j-],dp[j]) + grid[i][j];
}
}
}
return dp.back();
}
};

上面的比较容易理解,下面的代码其实是一个道理:

class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int m = grid.size();
int n = grid[].size();
vector<int> dp(n+,INT_MAX);
dp[] = ;
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
dp[j+] = min(dp[j],dp[j+]) + grid[i][j];
}
}
return dp.back();
}
};

【LeetCode练习题】Minimum Path Sum的更多相关文章

  1. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

  2. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  3. LeetCode 64. Minimum Path Sum(最小和的路径)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  4. [LeetCode] 64. Minimum Path Sum 最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. LeetCode 64 Minimum Path Sum

    Problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  6. 【leetcode】Minimum Path Sum(easy)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  7. Java for LeetCode 064 Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. 【题解】【矩阵】【DP】【Leetcode】Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. C#解leetcode 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  10. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

随机推荐

  1. UVa10653.Prince and Princess

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. shell编程while

    脚本编程:    顺序结构    选择结构        if        case    循环结构        for        while        until        whil ...

  3. Ajax 介绍

    Ajax的关键技术:  异步处理数据 使用XHTML(HTML)和CSS构建标准化的展示层 使用DOM(document object model)进行动态显示和交互 使用XML和XSLT进行数据交换 ...

  4. python部分排序算法(网友提供)

    // 冒泡排序 def bubble(x,n):    '''This function orders the original items x x is list,n is the length o ...

  5. QT绘制系统简介

    #3个类:QPainter,QPainterDevice 和 QPaintEngine 三个类 #qpainter用于执行绘制操作 #QPainterDevice是一个二维空间抽象,允许qpainte ...

  6. Subversion安装

    一.Subversion介绍 Subversion是一个集中式的信息共享系统.版本库是Subversion的核心部分,是数据的中央仓库.版本库以典型的文件和目录结构形式文件系统树来保存信息.任意数量的 ...

  7. POJ 3114 Countries in War(强连通+最短路)

    POJ 3114 Countries in War 题目链接 题意:给定一个有向图.强连通分支内传送不须要花费,其它有一定花费.每次询问两点的最小花费 思路:强连通缩点后求最短路就可以 代码: #in ...

  8. python-生成随机字符

    需求: 随机生成6位的大写字母: 方法一: #!/usr/bin/env python # -*- coding:utf-8 -*- import random li = [] for i in ra ...

  9. LESS使用方法简介(装逼神器)

    LESS 做为 CSS 的一种形式的扩展,它并没有阉割 CSS 的功能,而是在现有的 CSS 语法上,添加了很多额外的功能,所以学习 LESS 是一件轻而易举的事情,果断学习之! 变量 很容易理解: ...

  10. Ext tpl 造成 store不能正确加载

    最近维护别人写的代码的时候,遇到了这么个情况 找原因找到了这行代码的身上 tpl: '<tpl for="."><div ext:qtip="{name ...