Minimum Path Sum 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/minimum-path-sum/description/


Description

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.

Example 1:

[[1,3,1],
[1,5,1],
[4,2,1]]

Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.

Solution

class Solution {
public:
int minPathSum(vector<vector<int> >& grid) {
if (grid.empty())
return 0;
int i, j;
int rowCount = grid.size();
int colCount = grid[0].size();
int **stepCount;
stepCount = new int*[rowCount]; stepCount[0] = new int[colCount];
stepCount[0][0] = grid[0][0];
for (i = 1; i < rowCount; i++) {
stepCount[i] = new int[colCount];
stepCount[i][0] = grid[i][0] + stepCount[i - 1][0];
}
for (j = 1; j < colCount; j++) {
stepCount[0][j] = grid[0][j] + stepCount[0][j - 1];
} for (i = 1; i < rowCount; i++) {
for (j = 1; j < colCount; j++) {
stepCount[i][j] = min(stepCount[i - 1][j], stepCount[i][j - 1]) + grid[i][j];
}
}
j = stepCount[rowCount - 1][colCount - 1];
for (i = 0; i < rowCount; i++)
delete [] stepCount[i];
delete [] stepCount;
return j;
}
};

解题描述

这道题其实本质上类似于经典的动态规划问题中的最小编辑距离问题,大概的方法还是差不多的,通过一个矩阵去计算所有的状态下的步数,最后返回右下角的点的步数即为最小的步数之和。

[Leetcode Week9]Minimum Path Sum的更多相关文章

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

  2. 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 ...

  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

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

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

  6. 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 ...

  7. 【题解】【矩阵】【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 ...

  8. 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 ...

  9. 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. 【个人训练】(ZOJ3983)Crusaders Quest

    题意分析 和祖玛类似的那种玩法.不过是限定了九个字符,问最好情况下有几次三连碰. 暴力穷举即可.具体的做法是,先把所有"成块"的字符记录下来,然后一个一个删,再继续这样子递归做下去 ...

  2. Qt 报错LINK2019:无法解析的外部符号

    这里用的都是Qt 自己的东西,但是还是抱错,所以怀疑是没有包含进去,尝试了清理项目,重新编译等,还是不行 用到一个最好的办法,就是把构建的文件夹整个删除,在重新编译就可以了 如图所示,把debug和r ...

  3. centos7用yum搭建LAMP环境

    用yum快速搭建LAMP平台 实验环境: [root@nmserver- html]# cat /etc/redhat-release CentOS release (AltArch) [root@n ...

  4. Python学习笔记(二)一一一字典总结

    创建方式:1 直接创建     newDictonary={‘key’:'value',} 2 列表转字典(dict函数) 3 基本操作:len 返回总数 dictionary[k]  返回k对应的值 ...

  5. 可以随着SeekBar滑块滑动显示的Demo

    //关于Seek的自定义样式,之前也有总结过,但是,一直做不出随着滑块移动的效果,查询了很多资料终于解决了这个问题,现在把代码写出来有bug的地方 希望大家批评指正. Step 1 :自定义一个Vie ...

  6. 用IIS防止mdb数据库被下载(转载)

    原网址:http://www.cnblogs.com/kingreatwill/p/4224433.html 第一种方法:要求网站管理人员具体asp编程经验.因为现在的销售虚拟主机的系统,已经为用户建 ...

  7. [整理]docker内部时区修改的两种方法

    方法一 终端执行 date命令,查看宿主服务器的时区是否正确 如果正确: 执行 docker cp /etc/localtime 容器ID:/etc/localtime 将本地时间拷贝到docker内 ...

  8. Java 8手动实现一个Collector

    我们看一下Stream中的collect的方法. collect(toList())方法由Stream里的值生成一个列表,是一个及早求值的操作. Stream的of方法使用一个初始值生成新的Strea ...

  9. mysql与hive2.1.1安装和配置

    1.mysql安装 这个安装很简单,是在线安装,只需要按顺序执行一下几个命令就ok了. (1)sudo apt-get install mysql-server (2)sudo apt-get ins ...

  10. systemPath

    <dependency>   <groupId>com.aliyun.mns</groupId>   <artifactId>aliyun-sdk-mn ...