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.


题目标签:Array

  这道题目和前面两题差不多,基本思想都是一样的。这题要求我们找到最小和的路径。同样用Dynamic programming,我们可以分析一下,path 只能走右边和下面,那么对于每一个点,它的path 只可能从左边和上面来,我们只要在两者中取一个小的加上自己就可以了。因此,遍历gird,对于每一个点,有四种情况:

  case 1:它有left 和top,取小的加上自己;

  case 2:它只有left,left + 自己;

  case 3:它只有top, top + 自己;

  case 4:它没有left 和top,什么都不需要做。

  最后返回终点的值就可以了。这里可以in-place, 也可以自己新建立一个matrix。

Java Solution:

Runtime beats 35.80%

完成日期:07/22/2017

关键词:Array

关键点:Dynamic Programming, 逆向思考

 public class Solution
{
public int minPathSum(int[][] grid)
{
int rowSize = grid.length;
int colSize = grid[0].length; for(int i=0; i<rowSize; i++) // row
{
for(int j=0; j<colSize; j++) // column
{
// if this point has both left and top
if(j-1 >=0 && i-1 >=0)
grid[i][j] += Math.min(grid[i][j-1], grid[i-1][j]);
else if(j-1 >=0) // if this point only has left
grid[i][j] += grid[i][j-1];
else if(i-1 >=0) // if this point only has top
grid[i][j] += grid[i-1][j];
// else, this point has no left and no top }
} return grid[rowSize-1][colSize-1];
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 64. Minimum Path Sum(最小和的路径)的更多相关文章

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

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

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

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

  5. C#解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 ...

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

  7. [leetcode] 64. Minimum Path Sum (medium)

    原题 简单动态规划 重点是:grid[i][j] += min(grid[i][j - 1], grid[i - 1][j]); class Solution { public: int minPat ...

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

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

  9. 刷题64. Minimum Path Sum

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

随机推荐

  1. Linux下Birt、JTreeChart中文乱码问题解决办法

    Linux下JTreeChart,Birt等报表工具显示中文乱码解决 1) 现象: 在Windows上生成的报表图片展示正常,但是在Linux上显示(怪异的小方块“口”): 2)问题定位: 这是lin ...

  2. web自动化1-selenium简介及环境搭建

    selenium 开源软件 支持多浏览器Firefox.Chrome.IE 跨平台Windows.Mac.Linux 多语言 java python Ruby php js 对web支持好,多种API ...

  3. getOutputStream() has already been called for this response

    错误日志里偶尔会有getOutputStream() has already been called for this response这个错误 最近发现了高概率复现条件,所以顺手解决了一下: 首先根 ...

  4. 尝试在Linux上部署Asp.net Core应用程序

    快两个月没接触.net,倒是天天在用Linux,所以想尝试一下在Linux运行喜欢的.net 应用. 安装CentOS 安装.Net core for Linux 创建Asp.net Core应用程序 ...

  5. 【转载】关于api-ms-win-crt-runtimel1-1-0.dll缺失的解决方案

    关于api-ms-win-crt-runtimel1-1-0.dll缺失的解决方案 目录 关于api-ms-win-crt-runtimel1-1-0dll缺失的解决方案 目录 安装VC redite ...

  6. 51nod 1126 求递推序列的第N项 思路:递推模拟,求循环节。详细注释

    题目: 看起来比较难,范围10^9 O(n)都过不了,但是仅仅是看起来.(虽然我WA了7次 TLE了3次,被自己蠢哭) 我们观察到 0 <= f[i] <= 6 就简单了,就像小学初中学的 ...

  7. 简单说明CGI是什么

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  8. 不要62 hdu2089

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. poj2337欧拉回路要求输出路径

                         Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8368   Ac ...

  10. Ubuntu 16安装GPU版本tensorflow

    pre { direction: ltr; color: rgb(0, 0, 0) } pre.western { font-family: "Liberation Mono", ...