题目链接: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. poj3107 Godfather 求树的重心

    Description Last years Chicago was full of gangster fights and strange murders. The chief of the pol ...

  2. 【bzoj4897】[Thu Summer Camp2016]成绩单 区间dp

    题目描述 给你一个数列,每次你可以选择连续的一段,付出 $a+b\times 极差^2$ 的代价将其删去,剩余部分拼到一起成为新的数列继续进行此操作.求将原序列全部删去需要的最小总代价是多少. 输入 ...

  3. Ubuntu18.04 创建与编辑热点的方法

    在终端输入 nm-connection-editor 修改Hotspot,里边有热点名称及密码 当修改完了这些,要关闭热点,重新打开,这样才会生效!

  4. 🔺Count on a tree SPOJ - COT (无能为力。。。)

    https://cn.vjudge.net/problem/SPOJ-COT 插上 大佬的代码 和 我的...以后再看吧... Count on a tree 大佬:http://www.cnblog ...

  5. Linux环境安装.NET运行环境

    Linux环境安装.NET运行环境 Linux环境安装.NET运行环境 1. 构建编译环境: (1) sudo apt-get install build-essential (2) sudo apt ...

  6. BZOJ3261:最大异或和——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3261 给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.A x:添加 ...

  7. LINUX内核分析第四周——扒开系统调用的三层皮

    LINUX内核分析第四周--扒开系统调用的三层皮 李雪琦 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course ...

  8. Codeforces 601D. Acyclic Organic Compounds(四个愿望一次满足)

    trie合并的裸题...因为最多只有n个点,所以最多合并n次,复杂度$O(N*26)$. #include<iostream> #include<cstring> #inclu ...

  9. python创建多维列表

    By francis_hao    Mar 24,2018   "*"操作符可以用于列表,表示将列表内容重复n次.如下,   但是当列表内容是列表的时候就出问题了,如果我只是修改多 ...

  10. vmvare安装ubuntu后

    配置源: http://wiki.ubuntu.org.cn/%E6%BA%90%E5%88%97%E8%A1%A8#Trusty.2814.04.29.E7.89.88.E6.9C.AC 清理工作: ...