题目链接:https://leetcode.com/problems/minimum-path-sum/description/

题目大意:从左上到右下的路径中,找出路径和最小的路径(与62,63题相联系)。

法一:dfs,果然超时,无剪枝。代码如下:

     public int minPathSum(int[][] grid) {
boolean vis[][] = new boolean[grid.length][grid[0].length];
int f[][] = {{1, 0}, {0, 1}};
return dfs(grid, 0, 0, grid[0][0], Integer.MAX_VALUE, vis, f);
}
public static int dfs(int[][] grid, int x, int y, int sum, int res, boolean vis[][], int f[][]) {
if(sum >= res) {
return res;
}
if(x == grid.length - 1 && y == grid[0].length - 1) {
if(sum < res) {
res = sum;
}
return res;
}
for(int i = 0; i < 2; i++) {
int cnt_x = x + f[i][0];
int cnt_y = y + f[i][1];
if(cnt_x < grid.length && cnt_y < grid[0].length && vis[cnt_x][cnt_y] == false) {
vis[cnt_x][cnt_y] = true;
res = dfs(grid, cnt_x, cnt_y, sum + grid[cnt_x][cnt_y], res, vis, f);
vis[cnt_x][cnt_y] = false;
}
}
return res;
}

法二:dp,模仿62的二维dp,只是这里dp[i][j]表示到终点坐标为[i,j]的最短路径和,dp公式为dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + grid[i][j]。代码如下(耗时9ms):

     public int minPathSum(int[][] grid) {
int dp[][] = new int[grid.length][grid[0].length];
//初始化第一列
dp[0][0] = dp[0][0] = grid[0][0];
for(int i = 1; i < grid.length; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
//初始化第一行
for(int i = 1; i < grid[0].length; i++) {
dp[0][i] = dp[0][i - 1] + grid[0][i];
}
//计算dp
for(int i = 1; i < grid.length; i++) {
for(int j = 1; j < grid[0].length; j++) {
//取从左边和从上边到达的最短路径+当前值
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
}
}
return dp[grid.length - 1][grid[0].length - 1];
}

法三:一维dp,代码如下(耗时9ms):

     public int minPathSum(int[][] grid) {
int[] dp = new int[grid[0].length];
//初始化第一行
dp[0] = grid[0][0];
for(int j = 1; j < grid[0].length; j++) {
dp[j] = grid[0][j] + dp[j - 1];
}
//从第一行第0列开始计算
for(int i = 1; i < grid.length; i++) {
//计算第0列
dp[0] += grid[i][0];
//从第1列开始
for(int j = 1; j < grid[0].length; j++) {
dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];
}
}
return dp[grid[0].length - 1];
}

64.Minimum Path Sum---dp的更多相关文章

  1. 刷题64. Minimum Path Sum

    一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...

  2. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

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

  4. 64. Minimum Path Sum(中等, 又做出一个DP题, 你们非问我开不开心,当然开心喽!^^)

    Given an m x n grid filled with nonnegative numbers, find a path from top left to bottom right which ...

  5. 64. Minimum Path Sum (Graph; DP)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  6. [leetcode DP]64. Minimum Path Sum

    一个m*n的表格,每个格子有一个非负数,求从左上到右下最短的路径值 和62,63两个值是同一个思路,建立dp表,记录每个位置到右下角的最短路径的值 class Solution(object): de ...

  7. [LeetCode] 64. Minimum Path Sum 最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. LeetCode 64 Minimum Path Sum

    Problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  9. 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  10. LeetCode OJ 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

随机推荐

  1. [Code Festival 2017 qual A] B: flip

    题意 给出一个n行m列初始全白的矩阵,每次可以翻转一行/一列的全部格子的颜色.问任意次操作后能否使得恰好有k个黑色格子. n,m<=1000 分析 显然要么翻转一次要么不翻转. 最终黑色格子数只 ...

  2. [HDU5677]ztr loves substring

    ztr loves substring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  3. 转:解决Python中文编码问题

    Python 文本挖掘:解决Python中文编码问题 转于:http://rzcoding.blog.163.com/blog/static/2222810172013101785738166/   ...

  4. 【bzoj4559】成绩比较

    Portal -->bzoj4559 补档计划 ​  借这题补个档--拉格朗日插值 ​​  插值的话大概就是有一个\(n-1\)次多项式\(A(x)\),你只知道它在\(n\)处的点值,分别是\ ...

  5. NYOJ--520

    最大素因子 原题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=520 分析:先筛素数,同时记录下素数的序号,然后质因数分解. #include ...

  6. mesos+marathon+zookeeper+docker

    http://mesosphere.com/docs/mesosphere/getting-started/single-node-install/ mesos-master --zk=zk://lo ...

  7. java中16进制转换10进制

    java中16进制转换10进制 public static void main(String[] args) { String str = "04e1"; String myStr ...

  8. [DeeplearningAI笔记]卷积神经网络3.10候选区域region proposals与R-CNN

    4.3目标检测 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.10 region proposals候选区域与R-CNN 基于滑动窗口的目标检测算法将原始图片分割成小的样本图片,并传入分 ...

  9. jni里找不到刚添加的C++函数

    使用NDK开发,用到了JNI来连接C++和JAVA. 当C++方增加了一个新函数,jni访问此函数,eclipse会提示找不到改函数,然后前面打个红叉叉表示语法错误,从而阻碍了编译和运行. 当我选择清 ...

  10. Codeforces 797 D. Broken BST

    D. Broken BST http://codeforces.com/problemset/problem/797/D time limit per test 1 second memory lim ...