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. Linux中printk()实例

    新建hello.c #include <linux/kernel.h> #include <linux/module.h> int init_module(void) { pr ...

  2. Android的PopupWindow使用android学习之旅(四十三)

    PopupWindow简介 PopupWindow是一个类似dialog的控件,可以接受任何的view作为下拉列表显示. 用法 代码展示 package peng.liu.test; import a ...

  3. Mybatis事务(三)事务工厂

    在前面一篇博客Mybatis事务(一)事务管理方式中我们提到,mybatis及spring提供了三个事务实现类:JdbcTransaction.ManagedTransaction和SpringMan ...

  4. Java Web 高性能开发,第 1 部分: 前端的高性能

    Web 发展的速度让许多人叹为观止,层出不穷的组件.技术,只需要合理的组合.恰当的设置,就可以让 Web 程序性能不断飞跃.所有 Web 的思想都是通用的,它们也可以运用到 Java Web.这一系列 ...

  5. 不窃取用户隐私的搜索引擎: DuckDuckGo

    不窃取用户隐私的搜索引擎: DuckDuckGo https://duckduckgo.com/ 最近goggle不给力, baidu搜出来的很多都是垃圾, bing用久了很烦. 于是用上了DuckD ...

  6. iOS中 读取相册,调用系统相机 技术分享

    技术内容:分别读取相册以及调取相机,将图片显示到imageView上 布局: 1.创建imageView 和 button 并为button一个关联pickerImage的事件 <div sty ...

  7. PA 创建项目

    ---- 创建项目 DECLARE l_orig_project_id NUMBER := 6; l_prj_num VARCHAR2(240) := 'CXYTEST001'; l_start_da ...

  8. UILabel-UITextField-UIBotton&nbsp;UI_…

    注意:AppDelegate是类,所以self在这个类中指的就是AppDelegate对象 - (BOOL)application:(UIApplication *)application didFi ...

  9. 【Android 应用开发】Android 网络编程 API笔记 - java.net 包相关 接口 api

    Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...

  10. Ext.Net_1.X_WINDOW遮罩层被GridPanel挡住

    通过调试HTML代码,发现其实是DIV. chrome 中修改DIV Z:INDEX 就不被遮住了?但是又晓得如何修改window的Z:INDEX.那就修改"背景"GP的吧.