[Leetcode Week9]Minimum Path Sum
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的更多相关文章
- 【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 ...
- 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 ...
- [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 ...
- 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 ...
- 【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 ...
- 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 ...
- 【题解】【矩阵】【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 ...
- 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 ...
- 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 ...
随机推荐
- Qt Qml 汽车仪表
上一个原文连接http://blog.csdn.net/z609932088/article/details/53946245 参考资料连接:链接: https://pan.baidu.com/s/1 ...
- QC的使用学习(三)
一.需求转换测试 1.自动转换方法: (1)将最底层的子需求转换成设计步骤:即将最底层的子要求转换成测试用例的步骤. (2)将最底层的子要求转换成测试:即将最底层的要求转换成单个测试用例(建议使用) ...
- result returns more than one elements此种错误,解决
场景:公司产品开发完成后,接入第三方厂商,在进行接口联调的时候出现此问题.此接口报文中的每一个数据都要进行校验,有些是与已经存入产品数据库中的数据进行对比,看是否存在. 问题:在测试中,有些测试没有问 ...
- Spring实战第八章学习笔记————使用Spring Web Flow
Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...
- [Linux] 服务器镜像定时备份解决方案 crontab+rsync+flock
两台服务器定时同步文件解决方案: 环境: 主机:192.168.1.1 镜像机:192.168.1.2 需要将主机内容备份至镜像机(假设用户都为root) 备份内容为 /export 目录下所有内容至 ...
- 一些排序算法的Python实现
''' Created on 2016/12/16 Created by freeol.cn 一些排序算法的Python实现 @author: 拽拽绅士 ''' '''值交换''' def swap( ...
- deeplearning.ai课程学习(1)
本系列主要是我对吴恩达的deeplearning.ai课程的理解和记录,完整的课程笔记已经有很多了,因此只记录我认为重要的东西和自己的一些理解. 第一门课 神经网络和深度学习(Neural Netwo ...
- 基于规则的中文分词 - NLP中文篇
之前在其他博客文章有提到如何对英文进行分词,也说后续会增加解释我们中文是如何分词的,我们都知道英文或者其他国家或者地区一些语言文字是词与词之间有空格(分隔符),这样子分词处理起来其实是要相对容易很多, ...
- java正则表达式 3 -- 查找
用正则表达式执行查找命令,则需要用正则对象,其规则和执行顺序如下: 指定为字符串的正则表达式必须首先被便以为此类的实例.然后,可将得到的正则对象匹配任意的字符串用于创建Mather对象,执行匹配所涉及 ...
- 解决hadoop no dataNode to stop问题
错误原因: datanode的clusterID 和 namenode的 clusterID 不匹配. 解决办法: 1. 打开 hadoop/tmp/dfs/namenode/name/dir 配置对 ...