LintCode Minimum 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.
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的更多相关文章
- 【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 ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 【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 ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- 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 ...
- [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 ...
- 【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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- [Leetcode Week9]Minimum Path Sum
Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...
随机推荐
- hdoj 5139Formula
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5139 展开后的:1!*2!*3!*..*n! 后面就好办了- -
- 深入replace
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. stringObject.replace(reg/str,str/replacement); 这里主 ...
- AndroidManifest.xml file missing!
1.点击菜单栏中的Project——>Clean一下. 2.把AndroidManifest.xml文件再保存一下就可以了.
- 商品sku规格选择效果,没有商品的不能选中,选择顺序不影响展示结果
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Bootstrap<基础二十> 标签
Bootstrap 标签.标签可用于计数.提示或页面上其他的标记显示.使用 class .label 来显示标签,如下面的实例所示: <!DOCTYPE html> <html> ...
- 开启关闭keditor 过滤
filterMode: false, K.create('#txt_content', { uploadJson: '/js/kindeditor-4.1.10/upload_json.ashx', ...
- List<T>的对比
对于类型的对比,linq原来的对比是区分不了的. 对两个list进行查询交集方法.交集,并集的函数是直接用Linq的,这里不再写. List<T> intersectList = quer ...
- CSS兼容问题实用建议
CSS兼容问题,是美工最头痛的问题.做测试时,用谷哥和360浏览器(最新)都没有什么问题,用 IE6/IE8测试,问题就冒出来了.微软现在出IE10,我电脑上已经无法用IE6准确测试,IE-TESTE ...
- SharePoint Site "Regional Settings"功能与CSOM的对应
博客地址:http://blog.csdn.net/FoxDave SharePoint网站中的区域设置:"Regional Settings",可以用CSOM通过Site的一些 ...
- Linux内核分析——理解进程调度时机跟踪分析进程调度与进程切换的过程
20135125陈智威 +原创作品转载请注明出处 +<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验 ...