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.

Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.

题意:

给定一个二维矩阵, 找出一条从左上角到右下角的path,能使得这条path经过的所有数字相加之和最小

思路:

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

二维dp

1   4   5

2  ? dp[i][j]

6

初始化,

dp[0][0] = grid[0][0]

是否需要预处理第一个row: dp[0][j], 因为矩阵中间的dp[i][j]既可能来自上方,也可能来自左方。所以先预处理仅来自左方的path数字之和  dp[0][j] = dp[0][j-1] + grid[0][j]

是否需要预处理第一个col:dp[i][0],因为矩阵中间的dp[i][j]既可能来自上方,也可能来自左方。 所以先预处理仅来自上方的path数字之和  dp[i][0] = dp[i-1][0] + grid[i][0]

转移方程,

因为矩阵中间的dp[i][j]既可能来自上方,也可能来自左方,  要使得path的数字之和最小,必须比较上方和左方的结果哪个更小,再相加到当前的grid[i][j]上

dp[i][j] = min( dp[i-1][j], dp[j-1][i] ) + grid[i][j]

代码:

 class Solution {
public int minPathSum(int[][] grid) {
// init
int[][] dp = new int[grid.length ][ grid[0].length];
dp[0][0] = grid[0][0];
for(int i = 1; i< grid.length; i++){
dp[i][0] = grid[i][0] + dp[i-1][0] ;
} for(int j = 1; j< grid[0].length; j++){
dp[0][j] = grid[0][j] + dp[0][j-1] ;
}
// func
for(int i = 1; i< grid.length; i++){
for(int j = 1; j< grid[0].length; j++){
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j];
}
}
return dp[grid.length-1 ][ grid[0].length-1];
}
}

[leetcode]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] 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. 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 ...

  4. 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 ...

  5. 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 ...

  6. 064 Minimum Path Sum 最小路径和

    给定一个只含非负整数的 m x n 网格,找到一条从左上角到右下角的可以使数字之和最小的路径.注意: 每次只能向下或者向右移动一步.示例 1:[[1,3,1], [1,5,1], [4,2,1]]根据 ...

  7. C#解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 ...

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

  9. Leetcode64.Minimum Path Sum最小路径和

    给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [   [1,3,1], [1,5,1] ...

随机推荐

  1. The problems when using a new ubuntu 18.04

    how to install dual systems (windows & ubuntu) Donwloading the ubuntu from web. Using refu to cr ...

  2. liunx学习笔记(一:常用命令)

    linux: 在学习linux之前我们应该多少了解windows的一些相关操作,linux也就是类似windows的另一种操作系统,用来管理软硬件的一种应用.在windows下你可以通过鼠标点击相关的 ...

  3. Spring Boot/Spring Cloud

    104.什么是 spring boot?         在Spring框架这个大家族中,产生了很多衍生框架,比如 Spring.SpringMvc框架等,Spring的核心内容在于控制反转(IOC) ...

  4. 第2章 Java基本语法(上): 变量与运算符

    2-1 关键字与保留字 关键字(keyword) 保留字(reserved word) 2-2 标识符(Identifier) 案例 class Test{ public static void ma ...

  5. Dart Map<> 添加 元素

    Map<String, WidgetBuilder> routesList() { Map<String, WidgetBuilder> re = new Map<Str ...

  6. OpenLDAP一登录系统就修改密码

    1:修改配置文件 在前面打开注释 moduleload ppolicy.la modulepath /usr/lib/openldap modulepath /usr/lib64/openldap   ...

  7. Docker修改本地镜像与容器的存储位置

    这个方法里将通过软连接来实现. 首先停掉Docker服务: systemctl restart docker或者service docker stop 然后移动整个/var/lib/docker目录到 ...

  8. 【java】模板方法设计模式

    模板方法:在定义功能时,功能一部分是确认的,另一部分是不确认的或者后续会变化的.这时可以把不确定的部分暴露出去,定义成抽象类或者接口,由子类来完成. abstract class GetDuring ...

  9. python修改字典的值(update map value)

    mydict.update({'newkey':'newvalue'})

  10. java中的exception stack有时候不输出的原因(转)

    原文  https://www.cnblogs.com/lemonlotus/p/5650687.html 有时候,我们在看java错误日志时,只看到一个java.lang.NullPointerEx ...