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. bootstrap基础学习五篇

    bootstrap表格 Bootstrap 提供了一个清晰的创建表格的布局.下表列出了 Bootstrap 支持的一些表格元素: 标签 描述 <table> 为表格添加基础样式. < ...

  2. hdu 1140:War on Weather(计算几何,水题)

    War on Weather Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  3. 传递任意数量的实参*parameter&使用任意数量的关键字实参**parameter

    1.*形参名(*parameter) 有时候我们不知道知道函数需要接受多少个实参,所以我们可以在形参名前加一个*,是让python创建一个名为parameter的空元组,并将收到的所有值都封装到这个元 ...

  4. 自己动手写CPU之第九阶段(2)——载入存储指令说明2(lwl、lwr)

    将陆续上传新书<自己动手写CPU>.今天是第38篇,我尽量每周四篇,可是近期已经非常久没有实现这个目标了.一直都有事,不好意思哈. 开展晒书评送书活动,在q=%E4%BA%9A%E9%A9 ...

  5. 《C++ Primer Plus》第3章 处理数据 学习笔记

    C++的基本类型分为两组:一组由存储为证书的值组成,另一组由存储为浮点格式的值组成.整型之间通过存储键值时使用的呢存及有无符号来区分.整型从最小到最大依次是:bool,char,signed char ...

  6. LeetCode 笔记系列 14 N-Queen II [思考的深度问题]

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  7. js 模拟 select 的 click 事件

    法一. 你可以直接通过修改 select 的 size 属性来实现,但是这样比较丑,很明显:原来: 修改 size: 跟原生比丑在两点: 位置上移了 滚动条出现了 法二. 比较好的实现,就是通过 js ...

  8. js 高程 22.1.4 函数绑定 bind() 封装分析

    js 高程 书中原话(斜体表示): 22.1.4 函数绑定 另一个日益流行的高级技巧叫做函数绑定.函数绑定要创建一个函数,可以在特定的this 环境中 以指定参数调用另一个函数.该技巧常常和回调函数与 ...

  9. Navicat连接阿里云(centos7.3)的MySQL数据库遇到的问题及解决方法

    注:本文涉及到的解决方案都是我遇到的问题的对应解决方案,不一定适用于每一个人,如果问题仍然存在,请继续百度查询其他解决方法 1.  首先是登录阿里云MySQL的一些必要信息(登录其他云主机的mysql ...

  10. java面向对象、构造方法 之内部类

    UML类图 UML  Unified Modeling Language (UML)又称统一建模语言或标准建模语言 在一个类中      属性表示这类事物的性质(描述它的数据)      方法表示这类 ...