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,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.

 1 class Solution {
 2 public:
 3     int caluniquePaths(vector<vector<int>>&grid,vector<vector<int>>&mark,int m, int n) {
 4         int down = -1, right = -1;
 5         if (mark[m][n] != -1)return mark[m][n];
 6         int orim = grid.size(), orin = grid[0].size();
 7         if (m > 1)
 8             down = caluniquePaths(grid,mark, m - 1, n);
 9         if (n > 1)
10             right = caluniquePaths(grid,mark, m, n - 1);
11         if (down == -1)down = right;
12         if (right == -1)right = down;
13         mark[m][n] = grid[orim-m][orin-n]+min(down,right);
14         return mark[m][n];
15     }
16     int minPathSum(vector<vector<int>>& grid) {
17         int m = grid.size(), n = grid[0].size();
18         vector<vector<int>>mark(m+1, vector<int>(n+1, -1));
19         mark[1][1] = grid[m-1][n-1];
20         return caluniquePaths(grid,mark, m, n);
21     }
22 };

用的改的上一题的题解,但偏慢,不如改成dp快,估计是调用比较慢

 1 class Solution {
 2 public:
 3     int minPathSum(vector<vector<int>>& grid) {
 4         int m = grid.size(), n = grid[0].size();
 5         vector<vector<int>>dp(m, vector<int>(n, INT_MAX));
 6         dp[0][0] = grid[0][0];
 7         for(int i=0;i<m;i++)
 8             for (int j = 0; j < n; j++) {
 9                 if (i == 0 && j == 0)continue;
10                 if (i == 0) {
11                     dp[i][j] = min(dp[i][j - 1] + grid[i][j], dp[i][j]);
12                 }
13                 else if (j == 0) {
14                     dp[i][j] = min(dp[i - 1][j] + grid[i][j], dp[i][j]);
15                 }
16                 else {
17                     dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j], dp[i][j]);
18                 }
19             }
20         return dp[m - 1][n - 1];
21     }
22 };

原文地址:https://www.cnblogs.com/yalphait/p/10361215.html

时间: 2024-08-02 11:26:03

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

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. 思路:此题和前面几个机器人的题非常相像,只是变化了一点,具体代码和

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】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的格子,每一个格子上是一个非负整数,找