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. Linux学习--“杀死”程序

    (cp https://billie66.github.io/TLCL/book/chap11.html) While this is all very straightforward, there ...

  2. c#利用ApplicationContext类 同时启动双窗体的实现

    Application类(位于System.Windows.Forms命名空间)公开了Run方法,可以调用该方法来调度应用程序进入消息循环.Run方法有三个重载 1.第一个重载版本不带任何参数,比较少 ...

  3. 使用FileZilla连接时超时,无法连接到服务器

    更改一下加密方式,就是不用TLS,用相对不安全方式的(可选项)  腾讯云就是这样的,

  4. liunx top命令详解

    1,当前服务器时间,up,服务器离上一次重启过了多久,多少个用户在使用,cpu平均负载,grep 'core id' /proc/cpuinfo | sort -u | wc -l  ,一般来说4个, ...

  5. Day 12 开放封闭原则,装饰器初识

    nonlocal关键字 # 作用:将 L 与 E(E中的名字需要提前定义) 的名字统一​# 应用场景:如果想在被嵌套的函数中修改外部函数变量(名字)的值​# 案例:​def outer():    n ...

  6. Spring Cloud(Dalston.SR5)--Zuul 网关-过滤器

    Spring Cloud 为 HTTP 请求的各个阶段提供了多个过滤器,这些过滤器的执行顺序由各自提供的一个 int 值决定,提供的值越小则优先级越高,默认的过滤器及优先级如下: 自定义过滤器 在默认 ...

  7. Centos7创建CA和申请证书 转自https://www.cnblogs.com/mingzhang/p/8949541.html

    Centos7.3创建CA和申请证书 openssl 的配置文件:/etc/pki/tls/openssl.cnf 重要参数配置路径 dir   = /etc/pki/CA               ...

  8. 10K+,深度学习论文、代码最全汇总!

    我们大部分人是如何查询和搜集深度学习相关论文的?绝大多数情况是根据关键字在谷歌.百度搜索.想寻找相关论文的复现代码又会去 GitHub 上搜索关键词.浪费了很多时间不说,论文.代码通常也不够完整.怎么 ...

  9. k8s学习笔记之一:kubernetes简介

    一.虚拟化技术 1.什么是虚拟化技术 虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立 ...

  10. nowcoder 寻找(LCA)

    这个题貌似是过的最少的? smeow一眼给出了一个单log的算法orz 首先求出x和y的lca, x和c的lca,y和c的lca, 然后分类讨论以下就行了 实际上只有三种情况 #include< ...