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.

Notice

You can only move either down or right at any point in time!

Dynamic programming is ultilized to solve this problem.

First of all, define another matrix which has same dimension for both x and y. And let us define the number stored in the array stand for the minimum summation of all the path to the position with same x and y in grid matrix.

Then the sum[i][j] = min(sum[i-1][j],sum[i][j-1]) + grid[i][j];

Initialize the sum[0][0] = grid [0][0]; initialize the two boundaries with all the summation of previous path to that certain node.

Solve sum[m-1][n-1]

 public class Solution {
/**
* @param grid: a list of lists of integers.
* @return: An integer, minimizes the sum of all numbers along its path
*/
public int minPathSum(int[][] grid) {
// write your code here
if (grid == null || grid.length ==0 || grid[0].length == 0) {
return 0;
}
int m = grid.length;
int n = grid[0].length;
for(int i = 1; i < m; i++) {
grid[i][0] = grid[i-1][0] + grid[i][0];
}
for(int i = 1; i < n; i++) {
grid[0][i] = grid[0][i-1] + grid[0][i];
}
for(int i= 1; i < m; i ++) {
for (int j =1; j < n; j++) {
grid[i][j] = Math.min(grid[i-1][j],grid[i][j-1]) + grid[i][j];
}
}
return grid[m-1][n-1];
}
}

LintCode Minimum Path Sum的更多相关文章

  1. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  2. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

  3. 【LeetCode练习题】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  4. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  5. LeetCode: Minimum Path Sum 解题报告

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  6. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  7. 【LeetCode】64. Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  8. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  9. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

随机推荐

  1. tab事件优化-事件代理

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. MFC学习笔记(一)

    个人对MFC技术一直都很感兴趣,因为能够做出漂亮绚丽的界面应该是一件十分有成就感的事情. 学习的参考课本为北京博彦科技发展有限责任公司翻译的Jeff Prosise著的<MFC Windows程 ...

  3. liquibase的使用

    前言 liquibase是一个数据库持续集成插件.独立于数据库存在,oracle,mysql,db2,h2,sql server,postgresql都能使用.它使用配置文件来更新数据库结构,并加入版 ...

  4. 程序内部让用户直接上appstore评价游戏的链接地址以及跳转方法

    NSString *str = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore ...

  5. 查询SQLSERVER执行过的SQL记录

    SELECT TOP 1000 --创建时间 QS.creation_time, --查询语句 SUBSTRING(ST.text,(QS.statement_start_offset/2)+1, ( ...

  6. shell 随机从数组中抽取三个随机数(#可持续不停抽取)

    #!/bin/bash #b= ]] #do #sleep 1 student=( DPL YPD LT ZZM HY CQW LSJ ybr) a=$[RANDOM%+] c=$[RANDOM%+] ...

  7. php反射

    反射 //反射查找对象方法所在的文件名.$n_func = new ReflectionMethod($obj,$function);$filepath = $n_func->getFileNa ...

  8. 关于mongoldb 启动时显示 add already in use

    1 .不要在国内网上查找 浪费时间 2. stack over flow 是个不错的选择 进入正题. 终端输入: ps wuax | grep mongo 会看到: 随后:kill 447

  9. 利用border-radious画图形

    今天才发现,border-radius可以画很多图形,下面跟我来看一下吧: 在设有宽和高的情况下画一个圆: #div1{ /*宽高相等,圆角范围为高或宽的一半或以上*/ background-colo ...

  10. MySQL数据库7 - 汇总和分组数据

    一 汇总和分组数据 查询语句 ---> 结果集(多条数据) ---> 聚合函数  ----> 单行记录 1.常用的聚合函数: sum()         数字             ...