minimun path sum(最小路径和)
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(最小路径和)的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 064 Minimum Path Sum 最小路径和
给定一个只含非负整数的 m x n 网格,找到一条从左上角到右下角的可以使数字之和最小的路径.注意: 每次只能向下或者向右移动一步.示例 1:[[1,3,1], [1,5,1], [4,2,1]]根据 ...
- Leetcode64.Minimum Path Sum最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [1,3,1], [1,5,1] ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- Shell脚本编程入门(一)
最近在学shell,记录一下. if语句的使用: 1.判断两个参数大小 #!/bin/sh #a test about if statement a=10 b=20 if [ $a -eq $b ]; ...
- Cocos2D iOS之旅:如何写一个敲地鼠游戏(十):创建游戏逻辑
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- UNIX网络编程——套接字选项(心跳检测、绑定地址复用)
/* 设置套接字选项周期性消息检测连通性 心跳包. 心博.主要用于长连接. * 参数:套接字, 1或0开启, 首次间隔时间, 两次间隔时间, 断开次数 */ void setKeepAlive( in ...
- STL:list用法详解
list容器介绍 相对于vector容器的连续线性空间,list是一个双向链表,它有一个重要性质:插入操作和删除操作都不会造成原有的list迭代器失效,每次插入或删除一个元素就配置或释放一个元素空间. ...
- linux服务器批量部署应用系统shell脚本(Tomcat/jetty)
linux服务器批量部署应用系统shell脚本: 1.请更换代码内的服务器地址(Tomcat或jetty服务器) serverRoot=/home/undoner/java_tool/apache-t ...
- java常用集合类详解(有例子,集合类糊涂的来看!)
Framework集合框架是一个统一的架构,用来表示和操作集合.集合框架主要是由接口,抽象类和实现类构成.接口:蓝色:实现类:红色Collection|_____Set(HashSet)| ...
- Java实现简易的文件的迁移器
Java作为世界上最受欢迎的一门编程语言,自然是有原因的.比如说我们可以直接的方便的调用其中的函数来实现我们想要的功能. 一个偶然的机会,我浏览API文档时发现了一个名为FileDialog的类,然后 ...
- OC语言(六)
四十六.block类型 用来封装代码,可以在任何时候执行. 与函数的区别:可以在运行时动态产生. block的标志:^ 有形参.有返回值 int (^sumblock)(int,int) = ^(in ...
- Ubuntu 14 安装MySQL指南
ubuntu 14 安装MySQL指南安装MySQLsudo apt-get install mysql-server这个应该很简单了,而且我觉得大家在安装方面也没什么太大问题,所以也就不多说了,下面 ...
- CSS3 Media Queries 简介
原文链接:Introduction to CSS3 Media Queries 原文日期: 2014年2月21日 翻译日期: 2014年2月26日 翻译人员: 铁锚 简介 随着移动设备的日益普及,we ...