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 which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
代码:oj测试通过 Runtime: 102 ms
class Solution:
# @param grid, a list of lists of integers
# @return an integer
def minPathSum(self, grid):
# none case
if grid is None:
return None
# store each step on matrix
step_matrix=[[0 for i in range(len(grid[0]))] for i in range(len(grid))]
# first row
tmp_sum = 0
for i in range(len(grid[0])):
tmp_sum = tmp_sum + grid[0][i]
step_matrix[0][i] = tmp_sum
# first line
tmp_sum = 0
for i in range(len(grid)):
tmp_sum = tmp_sum + grid[i][0]
step_matrix[i][0] = tmp_sum
# dynamic program other positions in gird
for row in range(1,len(grid)):
for col in range(1,len(grid[0])):
step_matrix[row][col]=grid[row][col]+min(step_matrix[row-1][col],step_matrix[row][col-1])
# return the minimun sum of path
return step_matrix[len(grid)-1][len(grid[0])-1]
思路:
动态规划常规思路。详情见http://www.cnblogs.com/xbf9xbf/p/4250359.html这道题。
需要注意的是,动态规划迭代条件step_matrix[][]=grid[][]+min(...)
min里面的一定要是step_matrix对应位置的元素。一开始写成grid对应的元素,第一次没有AC,修改后第二次AC了。
leetcode 【 Minimum Path Sum 】python 实现的更多相关文章
- [leetcode]Minimum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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: 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 最小路径和
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 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 ...
- LeetCode Minimum Path Sum (简单DP)
题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. class Solution ...
- [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 ...
- [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 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
随机推荐
- 报错:无法打开"cocos-ext.h" /添加第三方库
参考原文:http://lin-jianlong.diandian.com/post/2012-11-05/40042951271 1.项目属性->配置属性->C/C++->常规-& ...
- 【MATLAB】画平行于坐标轴的曲线
hold on; ylim=get(gca,'Ylim'); % 获取当前图形的纵轴的范围 plot([4,4],ylim,'m--'); % 绘制x=4的直线 hold off;
- 在vim中插入命令行的输出结果
vim是linux中常见的编辑器,这里讲讲如何在vim中插入命令行的输出结果. 基本用法: 在指令模式下运行 :!command ,如!date将日期显示在vim底部,!ls列出当前目录 将命令结果插 ...
- pc-要实现相隔一定时间数据排序变化一次
有时候产品会有这种要求,就是展示的数据三天是正序的,一天是逆序的,解决是: 以某一个时间点为基准点,然后获取当前的时间,然后计算差值,分情况 //专利 JPView : function(Sorder ...
- HDU1195 双向BFS(或BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195 , 双向BFS或者直接BFS也可以过. 其实这道题只是单向BFS就可以过的,但是为了练算法,所以 ...
- java Vamei快速教程22 内存管理和垃圾回收
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 整个教程中已经不时的出现一些内存管理和垃圾回收的相关知识.这里进行一个小小的总结. ...
- Redis基础数据结构
Redis数据库中每个键值对都是由对象( c 的结构体对象)组成的. 数据库键总是一个字符串对象(string object) 数据库键的值可以使字符串对象.列表对象(list object).哈希对 ...
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getSer
这其实就是 获得应用的根url,比如说你的应用的根路径是 http://localhost:8080,那么你列出的代码就是为basePath赋值为 http://localhost:8080.具体点: ...
- C++之RAII惯用法
http://blog.csdn.net/hunter8777/article/details/6327704 C++中的RAII全称是“Resource acquisition is initial ...
- CentOS 7 防火墙 出现Failed to start iptables.service: Unit iptables.service failed to load
错误信息如下: [root]# service iptables start Redirecting to /bin/systemctl start iptables.service Failed t ...