[LeetCode]题解(python):064-Minimum Path Sum

题目来源:

  https://leetcode.com/problems/minimum-path-sum/



题意分析:

  给定一个m×n的非负矩阵,找到一条路使得从(0,0)到(m - 1,n - 1)经过的所有数字的和最小(类似上两题,只能向下和向上)。



题目思路:

  和上一题类似,用一个二维矩阵a[i][j]代表从(0,0)到(i,j)的最小和。那么a[i][j] = min(a[i-1][j],a[i][j -1]) + nums[i][j]。那么这题也是一个动态规划问题,只需要把a的整个表打出来就可以了。时间复杂度是O(m×n)。



代码(Python):

  

 1 class Solution(object):
 2     def minPathSum(self, grid):
 3         """
 4         :type grid: List[List[int]]
 5         :rtype: int
 6         """
 7         m,n = len(grid),len(grid[0])
 8         ans = [[0 for i in range(n)] for j in range(m)]
 9         ans[0][0] = grid[0][0]
10         for i in range(m):
11             for j in range(n):
12                 if i !=0 and j == 0:
13                     ans[i][j] = ans[i-1][j] + grid[i][j]
14                 elif i == 0 and j != 0:
15                     ans[i][j] = ans[i][j - 1] + grid[i][j]
16                 elif i != 0 and j != 0:
17                     ans[i][j] = min(ans[i - 1][j],ans[i][j - 1]) + grid[i][j]
18         return ans[m - 1][n - 1]



转载请注明出处:http://www.cnblogs.com/chruny/p/5008373.html

时间: 2024-10-14 18:41:26

[LeetCode]题解(python):064-Minimum Path Sum的相关文章

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]

LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && 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】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

Java for 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. 解题思路: dp问题,和上一题一样,JAVA实现如下: stati

【LeetCode】【Python】Minimum Path Sum

此题不难,可以用dfs来做,也可以用动态规划,但明显dfs性能不如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 ti

[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 --- 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] Unique Paths && Unique Paths II && 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" - 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