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 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.

SOLUTION 1:
相当基础的DP题目:
This is a simple DP.
表达式: D[i][j]: 从左下到本点的最小值
递推公式: D[i][j] = Math.mn(D[i - 1][j], D[i][j - 1]) + grid[i][j]
初始化: D[i][j] = grid[i][j].
终止条件:到达终点
// Solution 1: DP
public int minPathSum1(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
} int rows = grid.length;
int cols = grid[0].length;
int[][] D = new int[rows][cols]; // This is a simple DP.
// 表达式: D[i][j]: 从左下到本点的最小值
// 递推公式: D[i][j] = Math.mn(D[i - 1][j], D[i][j - 1]) + grid[i][j]
// 初始化: D[i][j] = grid[i][j].
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
D[i][j] = grid[i][j]; if (i == 0 && j != 0) {
D[i][j] += D[i][j - 1];
} else if (j == 0 && i != 0) {
D[i][j] += D[i - 1][j];
} else if (i != 0 && j != 0) {
D[i][j] += Math.min(D[i][j - 1], D[i - 1][j]);
}
}
} return D[rows - 1][cols - 1];
}
SOLUTION 2:
使用DFS + Memory也可以解决问题。当前到终点有2种方式,往右,往下,两种路线,取一个较小的路线就行了。
public class Solution {
// Solution 1: DP
public int minPathSum1(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
int rows = grid.length;
int cols = grid[0].length;
int[][] D = new int[rows][cols];
// This is a simple DP.
// 表达式: D[i][j]: 从左下到本点的最小值
// 递推公式: D[i][j] = Math.mn(D[i - 1][j], D[i][j - 1]) + grid[i][j]
// 初始化: D[i][j] = grid[i][j].
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
D[i][j] = grid[i][j];
if (i == 0 && j != 0) {
D[i][j] += D[i][j - 1];
} else if (j == 0 && i != 0) {
D[i][j] += D[i - 1][j];
} else if (i != 0 && j != 0) {
D[i][j] += Math.min(D[i][j - 1], D[i - 1][j]);
}
}
}
return D[rows - 1][cols - 1];
}
// Solution 2: DFS + memory.
public int minPathSum(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
int[][] memory = new int[grid.length][grid[0].length];
// Bug 1: forget to initilize
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
memory[i][j] = -1;
}
}
return dfs(grid, 0, 0, memory);
}
public int dfs (int[][] grid, int i, int j, int[][] memory) {
int rows = grid.length;
int cols = grid[0].length;
if (i >= rows || j >= cols) {
// 表示不可达
return Integer.MAX_VALUE;
}
// The base case: arrive the destination.
if (i == rows - 1 && j == cols - 1) {
return grid[i][j];
}
// 已经搜索过的点不需要重复搜索
if (memory[i][j] != -1) {
return memory[i][j];
}
int sum = grid[i][j];
// 开始dfs 可能的路径,目前我们只有2种可能
sum += Math.min(dfs(grid, i + 1, j, memory), dfs(grid, i, j + 1, memory));
// Record the memory
memory[i][j] = sum;
return sum;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dp/MinPathSum_1222_2014.java
LeetCode: Minimum Path Sum 解题报告的更多相关文章
- 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- LeetCode: Path Sum 解题报告
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- [leetcode]Minimum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...
随机推荐
- maven常见问题问答(转)
转自:http://www.oschina.net/question/158170_29368 1.前言 Maven,发音是[`meivin],"专家"的 意思.它是一个很好的项目 ...
- java struts2入门学习实例--用户注册
一.用户注册示例 register.jsp <%@ page language="java" contentType="text/html; charset=UT ...
- linux shell 脚本攻略学习18--grep命令详解
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是unix/linux中用于文本搜索 ...
- 如何设置页面自动刷新第一篇?? servlet setHeader("refresh","2")
import java.io.IOException; import java.util.Random; import javax.servlet.ServletException; import j ...
- servlet 中通过response下载文件
public class ResponseDemo3 extends HttpServlet { private static final long serialVersionUID = -52329 ...
- arping 帮助——翻译
[root@localhost ~]# arping --helparping: invalid option -- '-'Usage: arping [-fqbDUAV] [-c count] [- ...
- js跨域问题解释 使用jsonp或jQuery的解决方案
js跨域及解决方案 1.什么是跨域 我们经常会在页面上使用ajax请求访问其他服务器的数据,此时,客户端会出现跨域问题. 跨域问题是由于javascript语言安全限制中的同源策略造成的. 简单来说, ...
- ListView点击Item展开隐藏项(单项展开、多项展开、复杂布局时的展开处理)
手机屏幕毕竟有限,当我们要显示较多数据时便不得不舍去一些次要信息.将主要信息优先显示,也使显示效果更加简洁美观.遇到类似的需求,我们使用最多的就是 ListView ,而假设每次点击一个 Item 都 ...
- matlab入门笔记(六):编程基础之M文件
摘自<matlab从入门到精通>胡晓东 在Matlab中,用户可以在命令行中直接输入命令,从而以一种交互式的方式来编写程序.这种方式适用于命令行比较简单,输入比较方便,同时处理的问题较少的 ...
- [转]session和cookie的区别和联系,session的生命周期,多个服务部署时session管理
Session和Cookie的区别 对象 信息量大小 保存时间 应用范围 保存位置 Session 小量,简单的数据 用户活动时间+一段延迟时间(一般为20分钟) 单个用户 服务器端 Cookie 小 ...