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的格子,每一个格子上是一个非负整数,找到从左上角到右下角的路线,它所经过的格子的值的和最小,返回这个最小值。要求只能每一步只能向右或者向下移动。



【思路】

在前面的几个题目的基础上我们不难想出解决办法。

1. 对于第一行,A[0][j] += A[0][j-1];

2. 对于第一列,A[i][0] += A[i-1][0];

3. 对于其他值,A[i][j] += min(A[i-1][j],A[i][j-1]);

---->---->

通过这几个图我们可以看到求解的过程。



【java代码】

 1 public class Solution {
 2     public int minPathSum(int[][] grid) {
 3         int row = grid.length;
 4         int col = grid[0].length;
 5
 6         if(row == 0 || col == 0) return 0;
 7
 8         int[] dp = new int[col];
 9         dp[0] = grid[0][0];
10         for(int j = 1; j < col; j++)
11             dp[j] = grid[0][j] + dp[j-1];
12
13         for (int i = 1; i < row; i++){
14             dp[0] += grid[i][0];
15             for (int j = 1; j < col; j++){
16                 dp[j] = Math.min(dp[j-1], dp[j]) + grid[i][j];
17             }
18         }
19
20         return dp[col - 1];
21     }
22 }
时间: 2024-10-05 07:27:19

LeetCode OJ 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(最小和的路径)

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 这道题目和前面两题差不多,基本思想都是一样的

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 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];

62. 63. Unique Paths 64. Minimum Path Sum

1. A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' i

【LeetCode】064. 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 () class Solut