题目

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. Java字节流与字符流的区别详解

    字节流与字符流 先来看一下流的概念: 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入 ...

  2. 《Unity預計算即時GI》笔记:一、基本概念与一些设置

    说明 这篇文章是对<Unity預計算即時GI>这个系列文章的笔记. 基本概念 在Unity裡,可以用兩種不同的技術來計算全域光照GI或光源反射,就是烘焙全域光照(Baked GI)和預計算 ...

  3. hbase查询语法

    1.scan '表名',{FILTER=>"PrefixFilter('rowkey值')"} scan 'useractions',{FILTER=>"Pr ...

  4. LeetCode Remove Duplicates from Sorted Array删除整型数组中的重复元素并返回剩下元素个数

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向开头第一个,e往后遍历相同的 i ...

  5. python_45_目录编程

    #获取当前目录 import os print(os.getcwd()) #获取目录内容 import os print(os.listdir('C:\\Python27')) #创建目录 impor ...

  6. 将指定的form表单所有输入项转为json数据

    今天学习时,看到的将form表单中的输入数据转成json 的jquery代码,直接贴出来: $.fn.serializeJson=function(){ var serializeObj={}; va ...

  7. Repeater控件里面取不到CheckBox的值

    然后在后台需要取到,选中了那些 然后在后台怎么取也取不到,当时就纳闷了,然后开始怀疑自己的代码是不是错了,经过仔细一看,我的妈呀,加载事件了差一句话......整个人都不好了 加载事件差这句话不行,补 ...

  8. fstatfs/statfs详解

    [fstatfs/statfs系统调用]       功能描述:   查询文件系统相关的信息.     用法:   #include <sys/vfs.h>    /* 或者 <sy ...

  9. JS - OOP-继承的最佳实现方式

    如上图,使用第三种方式实现继承最好,也就是加了下划线的. 但是Object.create方法是ES6才支持的,所以,右边就写了一个实现其同样功能的函数.

  10. 三 python并发编程之多线程-重点

    一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍 二 开启线程的两种方式 #方式一 from th ...