LeetCode OJ--Minimum Path Sum **
https://oj.leetcode.com/problems/minimum-path-sum/
对一个grid从左上角到右下角的路径,求出路径中和最小的。
受之前思路的影响,就寻思递归,并且记录中间过程的数据,这样避免重复计算。但是超时了。
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
vector<vector<int> > sum;
if(grid.size()==)
return ;
int row = grid.size();
int col = grid[].size();
sum.resize(row);
for(int i = ;i<row;i++)
sum[i].resize(col);
return calcPath(,,grid,row,col,sum);
}
int calcPath(int xPos,int yPos,vector<vector<int> > &grid,int row,int col, vector<vector<int> > &sum)
{
if(xPos == row- && yPos == col-)
return grid[xPos][yPos];
int min1 = -,min2 = -;
if(xPos < row-)
if (sum[xPos+][yPos] == )
min1 = calcPath(xPos+,yPos,grid,row,col,sum);
else
min1 = sum[xPos+][yPos];
if(yPos < col-)
if(sum[xPos][yPos+] == )
min2 = calcPath(xPos,yPos+,grid,row,col,sum);
else
min2 = sum[xPos][yPos+];
if(min1 == -)
return min2;
if(min2 == -)
return min1;
return min1<min2?min1:min2;
}
};
其实,这个递归也是动态规划的思想。
但是,动态规划也可以用for循环做,于是清理思路,动态规划,for循环实现。
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
vector<vector<int> > sum;
if(grid.size()==)
return ;
int row = grid.size();
int col = grid[].size();
sum.resize(row);
for(int i = ;i<row;i++)
sum[i].resize(col); //initialize
sum[row-][col-] = grid[row-][col-];
for(int i = col-;i>=;i--)
sum[row-][i] += grid[row-][i] + sum[row-][i+];
for(int i = row-;i>=;i--)
sum[i][col-] += grid[i][col-] + sum[i+][col-]; for(int i = row-; i>=;i--)
for(int j = col-;j>=;j--)
{
int t1 = grid[i][j] + sum[i][j+];
int t2 = grid[i][j] + sum[i+][j];
sum[i][j] = t1<t2?t1:t2;
}
return sum[][];
} };
LeetCode OJ--Minimum Path Sum **的更多相关文章
- [Leetcode Week9]Minimum Path Sum
Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...
- 【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 OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Path Sum
Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...
- 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 】python 实现
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- [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 OJ 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【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 ...
随机推荐
- (68)zabbix windows性能计数器使用详解
概述 windows下的性能计数器让zabbix监控更加轻松,直接获取性能计数器的数值即可完成windows监控.性能计数器如下: 1 perf_counter["\Processor( ...
- jquery.imgpreload.min.js插件实现页面图片预加载
页面分享地址: http://wenku.baidu.com/link?url=_-G8miwbgDmEj6miyFtjit1duJggBCJmFjR2jky_G1VftD9eS9kwGOlFWAOR ...
- Juicer 轻量级javascript模板引擎
juicer是一个javascript轻量级模板引擎. 使用方法 编译模板并根据数据立即渲染出结果 1 juicer(tpl, data); 仅编译模板暂不渲染,返回一个可重用的编译后的函数 1 va ...
- 阿里云全国快递物流查询api接口
口地址: https://market.aliyun.com/products/56928004/cmapi021863.html?spm=5176.730005.productlist.d_cmap ...
- HDU - 5884 Sort (二分答案+贪心)
有n个数字,你需要把这n个数字合成一个数字,每次只能把k个数字合并成一个,花费为这k个数字的和. 给一个最大花费,问不超过这个最大花费的情况下,k的最小值. Sample Input 1 5 25 1 ...
- CodeForce--Benches
A. Benches There are nn benches in the Berland Central park. It is known that aiai people are curr ...
- 光学字符识别OCR-2
灰度聚类 接着我们就对图像的色彩进行聚类.聚类的有两个事实依据: 1.灰度分辨率 肉眼的灰度分辨率大概为40,因此对于像素值254和255,在我们肉眼看来都 只是白色: ...
- 如何用字体在网页中画icon
一.用css雪碧图 1.简介 CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许将一个页面涉及到的所有零星图片都包含到一张大图中, 利用CSS的“background- ...
- POJ 1609 Tiling Up Blocks
Tiling Up Blocks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4675 Accepted: 1824 ...
- 【Luogu】P2567幸运数字(容斥爆搜)
题目链接 先预处理出幸运数,把成倍数关系的剔掉,然后用容斥原理搜索一下. 这里的容斥很像小学学的那个“班上有n个同学,有a个同学喜欢数学,b个同学喜欢语文……”那样. #include<cstd ...