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 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.
Summary:
想要从m*n的整型数矩阵左上角走到右下角,每次只可以向右或向下移动一步,求路径上的整型数最小情况下的数字之和。
Solution:
1. 暴力法:枚举从左上角到右下角的所有路径,分别计算出路径和并比较。但效率过低且明显不可行,故不考虑。
2. 动态规划:由于只可以向右或向下移动,若已知(i, j)位置上一格(i - 1, j)以及左一格(i, j - 1)的路径数字之和,即可确定该位置的路径数字和:
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1])
初始化:dp[0][0] = grid[0][0]
dp[0][j] = dp[0][j - 1] + grid[0][j]
dp[i][0] = dp[i - 1][0] + grid[i][0]
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int row = grid.size(), col = grid[].size();
vector<vector<int>> dp(row, vector<int>(col));
for (int i = ; i < row; i++) {
for (int j = ; j < col; j++) {
if (!i && !j) {
dp[i][j] = grid[i][j];
}
else if (!i && j) {
dp[i][j] = dp[i][j - ] + grid[i][j];
}
else if (i && !j) {
dp[i][j] = dp[i - ][j] + grid[i][j];
}
else {
dp[i][j] = min(dp[i - ][j], dp[i][j - ]) + grid[i][j];
}
}
}
return dp[row - ][col - ];
}
};
3. 在法2的基础上优化空间
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int row = grid.size(), col = grid[].size();
vector<int> dp(col);
dp[] = grid[][];
for (int i = ; i < row; i++) {
for (int j = ; j < col; j++) {
if (!i && !j) {
dp[j] = grid[i][j];
}
else if (!i && j) {
dp[j] = dp[j - ] + grid[i][j];
}
else if (i && !j) {
dp[j] += grid[i][j];
}
else {
dp[j] = min(dp[j], dp[j - ]) + grid[i][j];
}
}
}
return dp[col - ];
}
};
LeetCode 64 Minimum Path Sum的更多相关文章
- [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 ...
- 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]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 (medium)
原题 简单动态规划 重点是:grid[i][j] += min(grid[i][j - 1], grid[i - 1][j]); class Solution { public: int minPat ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 刷题64. Minimum Path Sum
一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...
- 【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 ...
随机推荐
- 用Model-View-ViewModel构建iOS App(转)
转载自 Model-View-ViewModel for iOS [译] 如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller, 即MVC.MVC是构建iOS a ...
- 从点云到网格(二)VRIP介绍
VRIP(Volumetric Range Image Processing),顾名思义,是从深度图重建网格的一种方法.VRIP是Brian Curless和Marc Levoy在1996年提出来的方 ...
- oracle 函数
1.--dense_rank()分析函数(查找每个部门工资最高前三名员工信息) select * from (select deptno,ename,sal,dense_rank() over(par ...
- C#调用百度高精度IP定位API通过IP获取地址
API首页:http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip 1.申请百度账号,创建应用,获取密钥(AK) http://lbsyu ...
- JSF primefaces dataTable paginator 表格分页 问题
当第一次查询返回list列表,分页1,2,3.....这是选择2,当前页面停留在第2页. 当再次查询后,因为使用的ajax,结果更新了,但当前页面依旧是第2页. 可以在jsf页面,datatable的 ...
- thinkphp 3.2.3 动态修改conf配置文件
thinkphp 3.2.3 的C()方法能修改配置文件,但是是动态修改的,没有真正的更改文件. 我查了网上网友分享的方法,都不怎么合适,我就自己摸索写了一个,配置写到text.php中,我的目录如下 ...
- mybatis-generator运行命令
java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml -overwrite
- JS中的动态表格
写法一:(有点啰嗦) //--------------XML DOM--------------------------------------function addTR(){ //1.取三个框的值 ...
- Web API系列(三)统一异常处理
前面讲了webapi的安全验证和参数安全,不清楚的朋友,可以看看前面的文章,<Web API系列(二)接口安全和参数校验>,本文主要介绍Web API异常结果的处理.作为内部或者是对外提供 ...
- #pragma once与#ifndef #define ...#endif的区别
1. #pragma once用来防止某个头文件被多次include: #ifndef,#define,#endif用来防止某个宏被多次定义. 2. #pragma once是编译相关,就是说这个 ...