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.

Example 1:

[[1,3,1],
[1,5,1],
[4,2,1]]

Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.

 
走的方向决定了同一个位置不会走2次。
如果当前位置是(x,y)上一步来自哪呢?
上一步可能从左边【x-1,y】来,也可能从上边来[x,y-1] ,所以当前的最小路径就是 min(左边 + 当前值 , 上边+当前值) 
 
dp[0][0] = a[0][0]
dp[x][y] = min(dp[x-1][y]+a[x][y],+dp[x][y-1]+a[x][y])
 
 
 
 class Solution:
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
dp = []
for i in grid:
dp.append(i) for i in range(len(grid)):
for j in range(len(grid[0])):
if(i==0 and j ==0):
dp[i][j] = grid[i][j]
elif i==0 and j !=0 :
dp[i][j] = dp[i][j-1] + grid[i][j]
elif i!=0 and j==0:
dp[i][j] = dp[i-1][j] + grid[i][j]
else:
dp[i][j] = min(dp[i-1][j]+grid[i][j],dp[i][j-1]+grid[i][j])
return dp[i][j]

64. Minimum Path Sum(最小走棋盘 动态规划)的更多相关文章

  1. [LeetCode] 64. Minimum Path Sum 最小路径和

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

  2. [leetcode]64. Minimum Path Sum最小路径和

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

  3. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

  4. 刷题64. Minimum Path Sum

    一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...

  5. 【LeetCode】64. 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 ...

  6. LeetCode 64. Minimum Path Sum(最小和的路径)

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

  7. 64. Minimum Path Sum 动态规划

    description: Given a m x n grid filled with non-negative numbers, find a path from top left to botto ...

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

  9. LeetCode 64 Minimum Path Sum

    Problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

随机推荐

  1. 深入分析jquery解析json数据

    我们先以解析上例中的comments对象的JSON数据为例,然后再小结jQuery中解析JSON数据的方法. JSON数据如下,是一个嵌套JSON: {"comments":[{& ...

  2. zookeeper配置详解

    原文地址: http://itindex.net/detail/40187-zookeeper-%E7%AE%A1%E7%90%86%E5%91%98-%E7%AE%A1%E7%90%86 参数名 说 ...

  3. UE4关于编译配置的参考(Debug,DebugGame,Development,Shipping,Test等)

    https://docs.unrealengine.com/latest/CHN/Programming/Development/BuildConfigurations/index.html 编译配置 ...

  4. JavaScript作用域原理——作用域根据函数划分

    一.一个for实例 <p id="scope3" style="color:red"></p> var pscope3 = docume ...

  5. 面试题思考:解释一下什么叫AOP(面向切面编程)

    这种在运行时,动态地将代码切入到类的指定方法.指定位置上的编程思想就是面向切面的编程. AOP是Spring提供的关键特性之一.AOP即面向切面编程,是OOP编程的有效补充. 使用AOP技术,可以将一 ...

  6. IOS7开发~新UI学起(三)

    1.UITextView: A )      IOS7新增加的 UITextViewDelegate 方法: - (BOOL)textView:(UITextView *)textView shoul ...

  7. Zabbix-3.0.3结合Grafana-3.1.0给你想要的绘图

    导读 Grafana 是 Graphite 和 InfluxDB 仪表盘和图形编辑器.Grafana 是开源的,功能齐全的度量仪表盘和图形编辑器,支持 Graphite,InfluxDB 和 Open ...

  8. LeetCode-Lowest Common Ancestor of a Binary Tre

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  9. kindeditor在Java项目中的应用以及图片上传配置

    在官网下载Kindededitor的开发包   在项目中javaweb项目中导入kindeditor必须要使用的Jar包(用于文件上传,除非你的富文本编辑器不使用图片上传)jar包可以在官网的开发包中 ...

  10. java面试基础题------》Java 中的父子类静态代码块,代码块,构造方法执行顺序

    4.指出下面程序的运行结果. class A { static { System.out.print("1"); } public A() { System.out.print(& ...