【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 minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
思路:由于只能向两个方向走,瞬间就没有了路线迂回的烦恼,题目的难度大大的降低了。先得到到达最上边和最左边的最小路径,其他的就从上面点和左边点中选一个路径短的路径加上。
当然,也可以空间上更简单一些,只缓存一行的数据,然后一行行的更新。但我懒得写了...
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
if(grid.size() == )
{
return ;
}
vector<vector<int> > ans(grid.size(), vector<int>(grid[].size(), ));
ans[][] = grid[][];
for(int i = ; i < grid.size(); i++)
{
ans[i][] = ans[i - ][] + grid[i][];
}
for(int j = ; j < grid[].size(); j++)
{
ans[][j] = ans[][j - ] + grid[][j];
}
for(int i = ; i <grid.size(); i++)
{
for(int j = ; j <grid[].size(); j++)
{
ans[i][j] = min(ans[i - ][j], ans[i][j - ]) + grid[i][j];
}
}
return ans.back().back();
}
};
【leetcode】Minimum Path Sum(easy)的更多相关文章
- 【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 ...
- 【题解】【矩阵】【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 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- 【LeetCode】113. Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【LeetCode】666. Path Sum IV 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 【Leetcode】【Medium】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】437. Path Sum III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
随机推荐
- 网站全面采用UTF-8方法
先是概念的理解: Unicode/UCS的压缩形式--UTF8出现了,套用官方网站的首句话『UTF-8 stands for Unicode Transformation Format-8. It i ...
- 【bzoj1864】[ZJOI2006]三色二叉树
题目描述 输入 仅有一行,不超过500000个字符,表示一个二叉树序列. 输出 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. 样例输入 1122002010 样例输出 ...
- C语言的源程序字符集与执行字符集
我们程序文件的字符集就是我们写出来的.c扩展名的文件的字符集,这里用的是系统默认的 ANSI 字符集,如下图: 上面的字符集我们不关心,我们关心的是 源程序的字符集 和程序的 执行字符集 ,源程序的字 ...
- 锋利的jQuery书中推荐的几款插件
1.jQuery表单验证插件——Validation 2.jQuery表单插件——Form 3.模态窗口插件——SimpleModal 4.管理Cookie的插件——Cookie 5.jQuery U ...
- java使用split分隔,需要注意的点
String severName = "10.6.62.244"; System.out.println(severName.split(".").length ...
- Dp~Hrbust1426( 集训队的晚餐 )
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxsAAAPRCAYAAACVrbUbAAAgAElEQVR4nOzdW5Bcx33n+X7aiH3b2J
- iOS开发——项目篇—高仿百思不得姐
01 一.包装为导航控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewControll ...
- Unity3D客户端和Java服务端使用Protobuf
转自:http://blog.csdn.net/kakashi8841/article/details/17334493 前几天有位网友问我关于Unity3D里面使用Protobuf的方法,一时有事拖 ...
- 使用JavaScript在项目前台开发的58种常用小技巧
oncontextmenu="return false" :禁止右键 onselectstart="return false" : 禁止选取 onpaste = ...
- velocity使用知识总结
1.后台传递List<bean> ,前台循环获取 List<Lead> leads = leadService.getAllLeadLists(); mv.addObject( ...