Leetcode 64. Minmum 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问题。由于只能向下或者右走,所以通过 [i,j] 这个点的最短路径,要么是从[i-1,j]过来,要么从[i,j-1]过来。注意当i=0和j=0时这两个边界特例。

 1 class Solution(object):
 2     def minPathSum(self, grid):
 3         """
 4         :type grid: List[List[int]]
 5         :rtype: int
 6         """
 7         m = len(grid)
 8         n = len(grid[0])
 9         minSum = [[0]*n for x in range(m)]
10
11         for i in range(m):
12             for j in range(n):
13                 if i == 0 and j == 0:
14                     minSum[0][0] = grid[0][0]
15                 elif i == 0:
16                     minSum[i][j] = minSum[i][j-1] + grid[i][j]
17                 elif j == 0:
18                     minSum[i][j] = minSum[i-1][j] + grid[i][j]
19                 else:
20                     minSum[i][j] = min(minSum[i-1][j], minSum[i][j-1]) + grid[i][j]
21
22         return minSum[-1][-1]
时间: 2024-08-09 22:02:49

Leetcode 64. Minmum 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];

【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】#113. Path Sum II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22,