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 ...
随机推荐
- IOS Core Animation Advanced Techniques的学习笔记(一)
转载. Book Description Publication Date: August 12, 2013 Core Animation is the technology underlying A ...
- Codeforces Round #370 (Div. 2) E. Memory and Casinos (数学&&概率&&线段树)
题目链接: http://codeforces.com/contest/712/problem/E 题目大意: 一条直线上有n格,在第i格有pi的可能性向右走一格,1-pi的可能性向左走一格,有2中操 ...
- Android 设置ListView当前显示的item
项目中可能会有这种需求:动态设置ListView显示的item 这种需求可能会出现在不同的情况下,有的是打开页面就要显示在特定的位置,也有的是浏览列表时实时更新数据并且改变了集合中数据,或者是某种条件 ...
- Spring 通过配置文件注入 properties文件
当我们需要将某些值放入 properties文件 key=value 的方式,获取文件信息使用spring 注入的方式会变得很便捷 1. spring 配置文件需要导入 <?xml versio ...
- java.lang.UnsupportedClassVersionError出错
代码出错如下:java.lang.UnsupportedClassVersionError: cn/itcast/mybatis/first/MybatisFirst : Unsupported ma ...
- OC基础--Xcode 模板修改和文档安装
修改项目模板 项目模板就是创建工程的时候选择的某一个条目, Xcode会根据选择的条目生成固定格式的项目 如何修改项目模板 找到Xcode, 右键"显示包内容" 打开"/ ...
- JSP基础语法---九九乘法表-java jsp
<%@ page language="java" import="java.util.*" contentType="text/html; ch ...
- LINUX:read、array、declare
read:要读取来自键盘输入的变量 使用规则: read [-pt] variale 选项与参数: -p:后面接提示字符: -t:后面接等待的“秒数”: 如果read之后不加任何参数,直接加上变量名称 ...
- Linux快捷键和别名
一.设置别名 1使用命令行 alias 别名='命令'(只对本次登陆生效) 2.使用配置文件设置别名(永久生效) vi /root/.bashrc 打开系统别名配置文件,一般是用 ...
- ctags and vim
1,源码目录下第归检索. ctags -R * 2,搜索tag并用vim打开: vim -t <tag> 3,在vim 下的一些操作: Keyboard command Action Ct ...