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 ...
随机推荐
- iOS 10 之 网络权限带来的坑
症状 iOS 10 之后,陆陆续续地有用户联系我们,说新机第一次安装.第一次启动的时候,app 首屏一片空白,完全没数据.kill 掉重新打开就好了. 一开始以为是用户网络情况不好,但随着越来越多的用 ...
- 【java】详解I/O流
目录结构: contents structure [+] File类 I/O流体系 流的基本介绍 访问文件 转化流 DataInputStream和DataOutputStream 对象流 推回输入流 ...
- ORA-01917: user or role 'PDB_DBA' does not exist
在使用seed PDB创建新的PDB的时候,报了以下错误提示: SQL> create pluggable database pdb2 admin user admin1 identified ...
- Eclipse配置SQL Explorer插件和数据库
1.下载SQL Explore插件,地址:http://www.sqlexplorer.org/,下载第三个.复制到eclipse插件相应文件夹重新启动,下载RCP插件能够直接使用exe 2.下载JD ...
- [转]在windows上实现多个java jdk的共存解决办法
问题背景 公司项目中应用到的jdk环境为1.6,最近在家学习IntelliJ IDEA中sdk多环境配置时,想安装Jdk1.8,作为学习基础.那么问题来了,公司项目扩展不支持jdk1.8,为了既能满足 ...
- mysql数据库,创建只读用户
数据库当前只有一个root用户,需要创建一个只读帐户给其他使用,因使用者是使用数据库管理工具在其他主机访问,所以还要开户远程访问权限,操作步骤如下. 1. 使用现有的root用户登录到Mysql. m ...
- Jmeter录制HTTPS
Jmeter有录制功能,录制HTTPs需要增加一个证书配置,录制步骤如下: 1.打开jmeter,添加线程组.线程组右键,逻辑控制器>录制控制器 工作台 右键 非测试元件 >HTTP代理服 ...
- ThinkPHP在Apache和Nginx下去除index.php方法
由于项目需要,用ThinkPHP开发的程序链接要去除index.php下面说下如何解决.一.Nginx方法 由于nginx不支持PATH_INFO,所以需要进入linux终端找到nginx 的配置文件 ...
- [LeetCode] Read N Characters Given Read4 I & II
Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...
- mysql数据库TINYINT取值范围详解
分享下mysql中TINYINT的取值范围,很基础的一些内容. 在MySQL的数据类型中,Tinyint的取值范围是:带符号的范围是-128到127.无符号的范围是0到255(见官方<MySQL ...