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 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, 逆向思考

 1 public class Solution
 2 {
 3     public int minPathSum(int[][] grid)
 4     {
 5         int rowSize = grid.length;
 6         int colSize = grid[0].length;
 7
 8
 9         for(int i=0; i<rowSize; i++) // row
10         {
11             for(int j=0; j<colSize; j++) // column
12             {
13                 // if this point has both left and top
14                 if(j-1 >=0 && i-1 >=0)
15                     grid[i][j] += Math.min(grid[i][j-1], grid[i-1][j]);
16                 else if(j-1 >=0) // if this point only has left
17                     grid[i][j] += grid[i][j-1];
18                 else if(i-1 >=0) // if this point only has top
19                     grid[i][j] += grid[i-1][j];
20                 // else, this point has no left and no top
21
22             }
23         }
24
25         return grid[rowSize-1][colSize-1];
26     }
27 }

参考资料:N/A

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

时间: 2024-10-12 06:42:15

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

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 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. 这道题的要求是在m*n

leetcode 64. Minimum Path Sum(最小路径和)

很典型的动态规划题目 C++解法一:空间复杂度n2 1 class Solution { 2 public: 3 int minPathSum(vector<vector<int>>& grid) { 4 int m=grid.size(),n=grid[0].size(); 5 int dp[m][n]; 6 dp[0][0]=grid[0][0]; 7 for(int i=1;i<m;i++){ 8 dp[i][0]=dp[i-1][0]+grid[i][0];

LeetCode 64. Minimum Path Sum 20170515

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. 题目大意:给定一个一个m*n格的方针,每个方格都放一个非负数,找到

LeetCode 64. Minimum Path Sum Java

题目: 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. 题意及分析:给出一个m*n大的网格,每个网格中的数字都为非

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 minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:此题和前面几个机器人的题非常相像,只是变化了一点,具体代码和

19.2.11 [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 minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Example: Input: [   [1,3,1], [1,5

【Leetcode】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. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

Leetcode 动态规划 Minimum Path Sum

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Minimum Path Sum Total Accepted: 15789 Total Submissions: 50645My Submissions Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum

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 minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. [题目分析] 一个m x n的格子,每一个格子上是一个非负整数,找