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 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
随机推荐
- SpringBoot常用应用属性配置表
#========================================= #COMMON SPRING BOOT PROPERTIES # #This sample file is pro ...
- MVC下c#对接微信公众平台开发者模式
在ashx文件中进行HttpContext的处理: using System; using System.Collections.Generic; using System.Linq; using S ...
- 基于Dockerfile 构建redis5.0.0(包括持久化)及RedisDestopManager 监控
一 创建Dockerfile [root@zxmrlc docker]# mkdir redis [root@zxmrlc docker]# cd redis && touch Doc ...
- 2017.12.1 如何用java写出一个菱形图案
上机课自己写的代码 两个图形原理都是一样的 1.一共有仨个循环 注意搞清楚每一层循环需要做的事情 2.第一层循环:是用来控制行数 3.第二层循环控制打印空格数 4.第三层循环是用来循环输出星星 imp ...
- 使用Scanner类获取键盘输入的会员卡号,并将该数据存储在变量中,输出这个变量的信息
package come.one01;import java.util.Scanner; // 导入Scanner类public class One03 { public static void ma ...
- Java代码工具箱_用Set给List/Vector去重
参考 方法一:需要2个容器,1个迭代去重,1个作为结果容器. 此方法其实想法比较简单也是正常思路: package com.yonyou.test; import java.util.List; im ...
- 使用PinYin4j,获取汉字的拼音字母
需要导入的文件 <!-- 引入pinyin4J的依赖 --> <dependency> <groupId>com.belerweb</groupId> ...
- Java构造方法经典例题
1.在程序中,经常要对时间进行操作,但是并没有时间类型的数据.那么,我们可以自己实现一个时间类,来满足程序中的需要. 定义名为MyTime的类,其中应有三个整型成员:时(hour),分(minute) ...
- JDK和CGLIB动态代理原理区别
JDK和CGLIB动态代理原理区别 https://blog.csdn.net/yhl_jxy/article/details/80635012 2018年06月09日 18:34:17 阅读数:65 ...
- python2与python3下的base64模块
Python2的编解码 python2中程序数据类型默认为ASCII,所以需要先将数据解码(decode)成为Unicode类型,然后再编码(encode)成为想要转换的数据类型(gbk,utf-8, ...