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.

这个题是在unique paths的基础上的,条件和那题一样,只能向下走和向右走,不同的是,那题求的是总的路径数,这题求得是最小路径和。

经过那题,可以看出,我们可以新建一个数组(也可以直接使用题目的数组),用来存当前位置的最小路径和。p[i][j]表示i,j位置的最小路径和。可以看出,该位置的最小路径和等于上一个位置和前一个位置的两者最小路径和的最小值加上自己的数,也就是p[i][j]=grid[i][j]+Math.min(p[i-1][j],p[i][j-1])。  边界条件:边界上只有一条路径,所以他们的最小路径和就是上一个位置的最小路径和加上当前位置的数。见代码

class Solution {
public int minPathSum(int[][] grid) {
int m=grid.length;
int n=grid[0].length;
//用一个数组存每个位置的最小路径和
int[][] tmp=new int[m][n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++){
if(i==0&&j==0)
tmp[0][0]=grid[0][0];
//初始化,左上边界只有一条路径,所以就是当前位置和前面的和。
else if(i==0)
tmp[0][j]=tmp[0][j-1]+grid[0][j];
else if(j==0)
tmp[i][0]=tmp[i-1][0]+grid[i][0]; //非边界处就是上和左的最小值加上当前位置的数
else{
tmp[i][j]=grid[i][j]+Math.min(tmp[i-1][j],tmp[i][j-1]);
}
} return tmp[m-1][n-1];
}
}

minimun path sum(最小路径和)的更多相关文章

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

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

  4. 064 Minimum Path Sum 最小路径和

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

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

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

  6. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

  7. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  8. [LeetCode] 437. Path Sum III 路径和 III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. [LeetCode] 931. Minimum Falling Path Sum 下降路径最小和

    Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...

随机推荐

  1. Android初级教程实现电话录音

    需求:设置来电后自动录音. 首先设置一个按钮,代码很简单这里就不再给出. 建一个类,RecorderServicer extends Service package com.ydl.recorder; ...

  2. UNIX网络编程——TCP长连接与短连接的区别

    一.TCP短连接 我们模拟一下TCP短连接的情况,client向server发起连接请求,server接到请求,然后双方建立连接.client向server发送消息,server回应client,然后 ...

  3. 如何回滚请求<复制系统初始的数据>所处理的数据

    一.    问题提出 请求名称:复制系统初始的数据 参数:空 问题: 今天早上财务实施人员新配置了一个OU,然后在跑复制系统初始的数据报表的时候,不小心,不输入参数就直接跑. 报表先是报错. 接下来的 ...

  4. Mybatis事务(一)事务管理方式

    Mybatis管理事务是分为两种方式: (1)使用JDBC的事务管理机制,就是利用java.sql.Connection对象完成对事务的提交 (2)使用MANAGED的事务管理机制,这种机制mybat ...

  5. Libgdx1.6.2发布,跨平台游戏开发框架

    原文地址:www.libgdx.cn [1.6.2] API更改:TiledMapImageLayer位置由整型改为浮点类型. API更改:添加GLFrameBuffer 和 FrameBufferC ...

  6. 【翻译】在Ext JS应用程序中构建可维护的控制器

    原文:Building Maintainable Controllers in Ext JS Apps 你好You Had Me 你是Tearing Me Apart 模板We Dont Need t ...

  7. Chipmunk僵尸物理对象的出现和解决(七)

    首先判断问题出现在Star的类方法doStickShorterWork中,于是逐步分词注释代码,最后剩下如下代码: +(void)doStickShorterWork:(Stick *)stick{ ...

  8. Touch Handling in Cocos2D 3.x(七)

    在touchMoved方法中寻找触摸在父节点(CCScene)中的位置并且移动CCDragSprite到其父节点的相应位置中去. 在我们在动作中观赏拖放机制之前,我们需要使用这个新实现的类.打开Mai ...

  9. 认证模式之Basic模式

    HTTP协议规范中有两种认证方式,一种是Basic认证,另外一种是Digest认证,这两种方式都属于无状态认证方式,所谓无状态即服务端都不会在会话中记录相关信息,客户端每次访问都需要将用户名和密码放置 ...

  10. Zookeeper实现master选举

    使用场景         有一个向外提供的服务,服务必须7*24小时提供服务,不能有单点故障.所以采用集群的方式,采用master.slave的结构.一台主机多台备机.主机向外提供服务,备机负责监听主 ...