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 ...
随机推荐
- 海量数据处理算法(top K问题)
举例 有一个1G大小的一个文件,里面每一行是一个词,词的大小不超过16字节,内存限制大小是1M.返回频数最高的100个词. 思路 首先把文件分开 针对每个文件hash遍历,统计每个词语的频率 使用堆进 ...
- Android支持多国语言化Values命名
android多国语言文件夹文件汇总如下: 维吾尔文(中国):values-ug-rCN 中文(中国):values-zh-rCN 中文(台湾):values-zh-rTW 中文(香港):values ...
- Qt中事件分发源代码剖析
Qt中事件分发源代码剖析 Qt中事件传递顺序: 在一个应该程序中,会进入一个事件循环,接受系统产生的事件,并且进行分发,这些都是在exec中进行的. 下面举例说明: 1)首先看看下面一段示例代码: i ...
- centos7安装jdk,tomcat,msyql(MariaDB)
操作系统版本 CentOS Linux release 7.2.1511 (Core) 安装jdk 下载jdk-8u66-linux-x64.rpm上传到linux上 先改用户权限 然后 rpm -i ...
- 简单模拟 Spring
简单的理解Spring的实现过程,模拟了Spring的读取配置文件 项目结构
- 【翻译】Ext JS 6早期访问版本发布
早期访问版本是什么 如何参与 都包括什么 Sencha Ext JS 6 Sencha Pivot Grid Sencha Cmd 6 JetBrains IDE插件 反馈 原文:Announcing ...
- 工作中常用的Linux命令
1.从其他机器拷贝文件夹 格式: scp -r 文件夹名 用户名@机器名:/路径 范例: scp -rsearch work@zjm-testing-ps23.zjm.baidu.com:/home/ ...
- C 语言之银行ATM机界面
其实就是简单地对switch的用法,希望能给广大读者一些思路,写出自己的创意界面. #include <stdio.h> void main() { char SelectKey,Cred ...
- 【Unity Shaders】概述及Diffuse Shading介绍
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- [查阅]Dalvik opcodes
Dalvik opcodes Dalvik opcodes Author: Gabor Paller Vx values in the table denote a Dalvik register. ...