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. SpringCloud服务发现(Eureka)简介

    Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能. 为什么要使用Eure ...

  2. C++之异常处理

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  3. WebApi 异常处理解决方案

    1.继承ExceptionFilterAttribute类,重写OnException方法 public class WebApiExceptionFilterAttribute : Exceptio ...

  4. logback中MDC使用

    今天在项目发现别人写了很多MDC.put("taskid", "testThread/heart/main_heart");或者MDC.put("ta ...

  5. zookeeper未授权访问漏洞

    1.什么是zookeeper? ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,它是集群的管理者,监视着集群中各个节点的状态根据节点提交 ...

  6. js对用户信息加密传输 java后端解密

    1.加密采用服务端随机生成加密因子放入session中,传入登录或注册界面(每次进入都刷新) 2.页面中引入jquery.aes.js(这个js从网上下的坑比较多,引入先后顺序不一致都会报错,所以最后 ...

  7. 第十一课——codis-server的高可用,对比codis和redis cluster的优缺点

    [作业描述] 1.配置codis-ha 2.总结对比codis的集群方式和redis的cluster集群的优缺点 =========================================== ...

  8. BroPHP使用心得

    使用BroPHP 学习框架的时候,遇到了不少的问题. 一.修改了mysql 的表结构后,在程序中始终不体现表的修改. 给一个表添加了几个字段,在程序中对表添加数据,无论是用 select(),还是用 ...

  9. wordcount(C语言)

    写在前面 上传的作业代码与测试代码放在GitHub上了 https://github.com/IHHHH/gitforwork 本次作业用的是C语言来完成,因为个人能力与时间关系,只完成了基本功能,扩 ...

  10. python - 2 8 16进制/颜色/字符编码

    1.二进制 八进制 十六进制 二进制: bin() 0b10010八进制: oct() 0o10十进制: 1-100十六进制: hex() 0X53 BH 十进制转2, 8,16进制: >> ...