LeetCode Minimum Path Sum (简单DP)

题意:

  给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少?

思路:

  比较水,只是各种空间利用率而已。

  如果可以在原空间上作修改。

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

AC代码

  

  至少也要用O(m)的空间吧。

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

AC代码

时间: 2024-10-12 21:58:10

LeetCode Minimum Path Sum (简单DP)的相关文章

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

[leetcode]Minimum Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/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 r

Leetcode: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. 解题分析: 每次只能向下或者向

LeetCode: 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. SOLUTION 1: 相当基础

LeetCode -- Minimum Path Sum

Question: 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. Analysis: 给出一个由非负数构成的 m

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 minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. class Solution { public: int minP

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. 此题解法同Triangle 数字三角形.在此不再赘述. class

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. 首先每一个路径的上一个路径都是来自于其上方和左方 现将最上面的路径进行求和,最左边的路径进行求和

064 Minimum Path Sum

064 Minimum Path Sum 纯dp class Solution: # @param {integer[][]} grid # @return {integer} def minPathSum(self, grid): m = len(grid) if m == 0: return 0 n = len(grid[0]) for i in range(1, m): grid[i][0] += grid[i-1][0] for j in range(1, n): grid[0][j]