Minimum Path Sum [LeetCode]
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: DP, calucate the minimun sum from start point to every cell unitil we traverse every cell, then just output the last cell.
void goRight(int row_index, int column_index, int **grid_sum, vector<vector<int> > &grid) {
column_index ++ ;
int sum = grid_sum[row_index][column_index - ] + grid[row_index][column_index];
if(grid_sum[row_index][column_index] == - || sum < grid_sum[row_index][column_index])
grid_sum[row_index][column_index] = sum;
}
void goDown(int row_index, int column_index, int **grid_sum, vector<vector<int> > &grid ) {
row_index ++;
int sum = grid_sum[row_index -][column_index] + grid[row_index][column_index];
if(grid_sum[row_index][column_index] == - || sum < grid_sum[row_index][column_index])
grid_sum[row_index][column_index] = sum;
}
int minPathSum(vector<vector<int> > &grid) {
if( grid.size() <= || grid[].size() <=){
return ;
}else if ( grid.size() == ){
int sum = ;
for (auto item : grid[])
sum += item;
return sum;
}else if (grid.size() > && grid[].size() == ) {
int sum = ;
for ( auto item : grid)
sum += item[];
return sum;
}else {
int row_size = grid.size();
int column_size = grid[].size();
int ** grid_sum = new int *[row_size];
for( int i=; i< row_size; i++ )
{
grid_sum[i] = new int [column_size] ;
}
for(int i = ; i< row_size; i ++){
for(int j =; j < column_size; j++)
grid_sum[i][j] = -;
}
for(int i = ; i <= (row_size - + column_size -); i++) {
if (i == ) {
int row_index = ;
int column_index = ;
grid_sum[][] = grid[][];
if(row_index + < row_size)
goDown(row_index, column_index, grid_sum, grid);
if(column_index + < column_size)
goRight(row_index, column_index, grid_sum, grid);
}else {
int row_index = ;
int column_index = ;
for(row_index = ; row_index <= i; row_index ++ ){
if(row_index >= row_size )
continue;
column_index = i - row_index;
if(column_index >= column_size)
continue;
if(row_index + < row_size)
goDown(row_index, column_index, grid_sum, grid);
if(column_index + < column_size)
goRight(row_index, column_index, grid_sum, grid);
}
}
}
int sum = grid_sum[row_size - ][column_size - ];
for( int i=; i< row_size; i++ ) {
delete [] grid_sum[i];
}
delete grid_sum;
return sum;
}
}
Minimum Path Sum [LeetCode]的更多相关文章
- Minimum Path Sum——LeetCode
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Minimum Path Sum leetcode java
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- [Leetcode Week9]Minimum Path Sum
Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...
- LeetCode 64. 最小路径和(Minimum Path Sum) 20
64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
- Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum)
Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum) 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. ...
- 【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练习题】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 ...
随机推荐
- 如何安装mysql服务
我刚开始安装mysql的时候,在windows的服务里面可以看到,但是装了以后有一段时间没有用它了,我在准备从windows的服务里面启动mysql服务的时候,发现没有mysql的服务了,那我的解决办 ...
- 正则表达式获取字符串中的img标签中的url链接
废话不多说直接看代码 JavaScript中的代码: var re = /src=\"([^\"]*?)\"/i; var arr = str.match(re); if ...
- PL/SQL 存储函数和过程
--存储过程 .不带参: create or replace procedure 存储过程名 as|is --说明部分 begin --执行的语句: end: 调用存储过程 execute 存储过程名 ...
- copy file to docker from realhost
http://blog.e3rp4y.me/blog/2014/05/23/copy-file-from-host-to-docker.html --------------------------- ...
- LaTex学习笔记——LaTeX公式换行
ps. 1.“\!” 表示其前后字符之间无间隙 2.暂留 3.段落中的数学表达式应该置于( 和), $ 和$ 或者begin{math} 和end{math} 之间. ...
- Leetcode: Minimum Genetic Mutation
A gene string can be represented by an 8-character long string, with choices from "A", &qu ...
- Hosts简单说明
Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联"数据库",当用户在浏览器中输入一个需要登录的网址时,系统 ...
- iOS-打开word、ppt、pdf、execl文档方式
这里面包括下载和打开文档的操作:需要先导入<AFNetworking>的框架 第一步:创建一个显示文档的view:ReadViewController (1).h的代码如下: @inter ...
- Java接口回调
public class A { private D d; private C c; public A (C c) { this.c = c; } public void setD (D d) { t ...
- IOS Xcode7 新建PCH文件
第一步:新建文件找到iOS中的Othere点击PCH File 点击Next 第二步:修改文件名为当前工程名(一般与工程名同名),勾选Targets,点击create创建 第三步:如图选中红框中的路径 ...