Minimum Path Sum Total Accepted: 34975 Total Submissions: 109325

题目来自于:LeetCode

https://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 right at any point in time.

Show Tags

Have you met this question in a real interview?

int minPathSum(int **grid, int nRows, int nCols) {
    if(nRows==0)
    return 0;
    int sum=0;
    if(nRows==1)
    {
        for(int i=0;i<nCols;++i)
        sum+=grid[0][i];
        return sum;
    }
      if(nCols==1)
    {
        for(int i=0;i<nRows;++i)
        sum+=grid[i][0];
        return sum;
    }
    for(int i=nRows-2;i>=0;--i)
       grid[i][nCols-1]+=grid[i+1][nCols-1];
       for(int j=nCols-2;j>=0;--j)
        grid[nRows-1][j]+=grid[nRows-1][j+1];
    for(int i=nRows-2;i>=0;--i)
       for(int j=nCols-2;j>=0;--j)
      {
          if(grid[i+1][j]<grid[i][j+1])
            grid[i][j]+=grid[i+1][j];
            else
            grid[i][j]+=grid[i][j+1];
       }
       return grid[0][0];
}
时间: 2024-10-13 15:59:02

Minimum Path Sum Total Accepted: 34975 Total Submissions: 109325的相关文章

Count Primes Total Accepted: 831 Total Submissions: 6167

题目来自:Leetcode https://leetcode.com/problems/count-primes/ Count Primes Total Accepted: 831 Total Submissions: 6167My Submissions Question  Solution Description: Count the number of prime numbers less than a non-negative number, n Hint: The number n c

leetcode_64题——Minimum Path Sum(动态规划)

Minimum Path Sum Total Accepted: 38669 Total Submissions: 120082My Submissions Question Solution 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. Not

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] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/unique-paths/ 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 rea

LeetCode之“动态规划”:Minimum Path Sum &amp;&amp; Unique Paths &amp;&amp; Unique Paths II

之所以将这三道题放在一起,是因为这三道题非常类似. 1. 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 righ

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】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 解题报告

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 &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