题目:

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.

代码:

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

tips:

典型的“DP+滚动数组”,时间复杂度O(m*n),空间复杂度O(n)。

=============================================

第二次,用偷懒的做法了,二维dp直接写了。

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

【Minimum Path Sum】cpp的更多相关文章

  1. leetcode 【 Minimum Path Sum 】python 实现

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

  2. 【Binary Tree Maximum Path Sum】cpp

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  3. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

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

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

  6. 【LeetCode】64. 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 ...

  7. 【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

    [064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with ...

  8. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

  9. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

随机推荐

  1. mui蒙版使用例子

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name=& ...

  2. 【Linux/Ubuntu学习 10】unbuntu 下 eclipse 中文乱码的解决

    wangdd@wdd-pc:~$ gedit /var/lib/locales/supported.d/local 添加: zh_CN.GBK GBK zh_CN.GB2312 GB2312 终端执行 ...

  3. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:5.技术简介之Hibernate

    目录 序言 配置 hibernate.cfg.xml配置文件 加载hibernate.cfg.html配置文件并获取Session 对象的注解配置 增删改查 具体的增删改查代码 数据库操作的封装 连接 ...

  4. php之基础深入---类与对象篇

    1.类的自动加载: spl_autoload_register()函数可以注册任意数量的自动加载器,当使用尚未被定义的类(class)和接口(interface)时自动去加载,这样可以避免includ ...

  5. Problem G: 圆周率

    Problem G: 圆周率 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 155  Solved: 99[Submit][Status][Web Bo ...

  6. cf1151 B

    题目连接 : https://codeforces.com/contest/1151/problem/B 可能我想法有问题,我怎么感觉B题的思路不直接想出来的,我想了一会才想出来,感觉不难,但可能有更 ...

  7. python读取图像

    from PIL import Imageimg = Image.open('/Users/NaCl/Desktop/test.png')img.show()

  8. 什么是SAD,SAE,SATD,SSD,SSE,MAD,MAE,MSD,MSE?

    SAD(Sum of Absolute Difference)=SAE(Sum of Absolute Error)即绝对误差和 SATD(Sum of Absolute Transformed Di ...

  9. Java代码工具箱之超出游标最大数

    1. Java大量写入oracle时容易出现此错.经过此错,也触动自己要深刻理解 java 的 prepareStatement 等对象,及数据库的连接与释放. 2. 原因:经常会出现在 for 循环 ...

  10. IE脚本调试

    打开IE -- 工具 -- Internet选项 -- 高级 --有4项. 1.禁用脚本调试(Internet Explorer)(去掉对勾) 2.禁用脚本调试(其他)(去掉对勾) 3.显示每个脚本错 ...