You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
解决问题的思路很重要,直接影响效率。 我想到的是递归, 别人用的是遍历
classSolution(object):
defminPathSum(self, grid):
""" :type grid: List[List[int]] :rtype: int """ifnotgridorgrid[0] == []: return0R, C=len(grid), len(grid[0])
foriinrange(R):
forjinrange(C):
ifi!=0orj!=0:
grid[i][j] +=min(grid[i-1][j] ifi>0elsefloat("Inf"),grid[i][j-1] ifj>0elsefloat("Inf"))
returngrid[-1][-1]
''' def __init__(self): self.min_val = float("Inf") def minPathSum(self, grid): """ :type grid: List[List[int]] :rtype: int """ m = len( grid ) n = len( grid[0] ) sum_val = 0 self.direction(0, 0, m, n, grid, sum_val) return self.min_val def direction(self, i, j, m, n, grid, sum_val): sum_val = sum_val + grid[i][j] if sum_val >= self.min_val: return if i==m-1 and j==n-1: if sum_val < self.min_val: self.min_val = sum_val if i < m-1 : self.direction(i+1, j, m, n, grid, sum_val) if j < n-1: self.direction(i, j+1, m, n, grid, sum_val) '''