题目

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 实现的更多相关文章

  1. [leetcode]Minimum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...

  2. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. LeetCode:Minimum Path Sum(网格最大路径和)

    题目链接 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

  7. LeetCode Minimum Path Sum (简单DP)

    题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. class Solution ...

  8. [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 ...

  9. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

  10. LeetCode 64. 最小路径和(Minimum Path Sum) 20

    64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...

随机推荐

  1. ArcGIS中经纬度数据转空间数据

    已有这样一批数据,后缀为.txt 或者.csv .现需要将其转化为空间数据. 转换过程: 打开ArcMap选择file——>Add Data——>Add XY Data 设置经纬度对应的字 ...

  2. Windows 服务快捷启动命令,可以直接在运行处运行电脑的功能。

    gpedit.msc-----组策略 sndrec32-----录音机 nslookup----- ip地址侦测器 explorer------ 打开资源管理器 logoff-------注销命令 t ...

  3. IOS类似9.png

    图形用户界面中的图形有两种实现方式,一种是用代码画出来,比如Quartz 2D技术,狠一点有OpenGL ES,另一种则是使用图片. 代码画的方式比较耗费程序员脑力,CPU或GPU; 图片则耗费磁盘空 ...

  4. IOS 某个控件出不来原因(经验分享)

    某个控件出不来:(检查原因) 1.frame的尺寸和位置对不对 2.hidden是否为YES 3.有没有添加到父控件中 4.alpha 是否 < 0.01 5.被其他控件挡住了 6.父控件的前面 ...

  5. 为项目创建podfile

    由于写项目 不常用到,容易忘记,记录一下 第一步:新建一个项目: 第二步:打开终端,输入 cd 第三步:把项目拖入终端,(获取项目路径) 第四步:回车,输入 pod init (生成podfile 文 ...

  6. 2018.5.28 Oracle数据库补充

    select * from (select rownum rn,e2.* from (select e1.* from emp e1)e2 where rownum<=10)e3 where e ...

  7. BZOJ3679: 数字之积(数位dp)

    题意 题目链接 Sol 推什么结论啊. 直接大力dp,$f[i][j]$表示第$i$位,乘积为$j$,第二维直接开map 能赢! /* */ #include<iostream> #inc ...

  8. SAP 各模块常用的BAPI

    MM模块 1. BAPI_MATERIAL_SAVEDATA 创建物料主数据 注意参数EXTENSIONIN的使用,可以创建自定义字段 例如:WA_BAPI_TE_MARA-MATERIAL = IT ...

  9. Atlas 配置高可用

    keepalived安装 #下载keepalived ./configure --prefix=/usr/local Make && make install Atlas主安装keep ...

  10. django+xadmin在线教育平台(五)

    3-3 django orm介绍与model设计 上节教程完成后代码(来学习本节前置条件): 对应commit: 留言板前端页面展示.本次内容截止教程3-2结束. 可能现在你还在通过手写sql语句来操 ...