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 解题报告的更多相关文章

  1. 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  4. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  5. 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...

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

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

  8. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  9. [leetcode]Minimum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...

随机推荐

  1. maven-compiler-plugin升级到3.1出现问题(转)

    转自:http://my.oschina.net/zhuka/blog/124503 No compiler is provided in this environment. Perhaps you ...

  2. lu协程练习

    生产者和消费者问题:当协程调用yield时,从一个悬而未决的resume中返回.简单的协程练习: function receive() local status,value = coroutine.r ...

  3. arping 帮助——翻译

    [root@localhost ~]# arping --helparping: invalid option -- '-'Usage: arping [-fqbDUAV] [-c count] [- ...

  4. fedora装机运行第一脚本

    博主原创: #!/bin/bash echo "更换源&更新源......" yum install wget -y yum install yum-fastestmirr ...

  5. Android Studio多Module开发需要注意的问题

    多module开发,其中的一个为入口module,其他module为独立的“应用”(library) 1.在原有的项目导入另外个项目的module为主项目的次module,即在A项目中添加一个启动B项 ...

  6. linux文件系统 - 初始化(一)

    术语表: struct task:进程 struct mnt_namespace:命名空间 struct mount:挂载点 struct vfsmount:挂载项 struct file:文件 st ...

  7. 还没被玩坏的robobrowser(8)——robobrowser的实现原理

    背景 学习使用工具实际上不难,不过我们应该通过阅读工具源码来提升自己的水平. 多读代码,读好代码.很不错,robobrowser的代码简单易懂,值得学习. 预备知识 源码地址 一起其实是从browse ...

  8. Groovy 学习手册(5)

    8. 函数式编程 函数式编程(FP)是一种编程风格,侧重于函数和最小化状态的变化(使用不可变的数据结构).它更接近于用数学来表达解决方案,而不是循序渐进的操作. 在函数式编程里,其功能应该是" ...

  9. [转]同步对象Event的用法

    同步对象Event的用法  首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,线程锁定方面.  CreateEvent函数功能描述:创建或打开一个命名的或无名的事 ...

  10. 解决 p0sixspwn-v1.0.4 win版 无法定位程序输入点sqlite3_wal_checkpoint的问题

    p0sixspwn-v1.0.4 win版今天早晨发现大神( @winocm · @iH8sn0w · @SquiffyPwn) 已经发布. 下载下来运行之,发现会报错: 无法定位程序输入点sqlit ...