File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode-cn.com/problems/minimum-path-sum/
3+ 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
4+
5+ 说明:每次只能向下或者向右移动一步。
6+
7+ 示例:
8+
9+ 输入:
10+ [
11+ [1,3,1],
12+ [1,5,1],
13+ [4,2,1]
14+ ]
15+ 输出: 7
16+ 解释: 因为路径 1→3→1→1→1 的总和最小。"""
17+
18+
19+ class Solution :
20+ """动态规划法"""
21+ def minPathSum (self , grid ):
22+ for i in range (len (grid )):
23+ for j in range (len (grid [0 ])):
24+ if i and j :
25+ grid [i ][j ] = min (grid [i ][j - 1 ], grid [i - 1 ][j ]) + grid [i ][j ]
26+ elif j :
27+ grid [i ][j ] = grid [i ][j - 1 ] + grid [i ][j ]
28+ elif i :
29+ grid [i ][j ] = grid [i - 1 ][j ] + grid [i ][j ]
30+ return grid [- 1 ][- 1 ]
Original file line number Diff line number Diff line change 7474
7575&emsp ;&emsp ; 015 [ 最大子序和] ( https://github.com/SimmyZhong/leetCode/blob/master/golang/53_maximum_subarray.go )
7676
77+ &emsp ;&emsp ; 064 [ 最小路径和] ( https://github.com/SimmyZhong/leetCode/blob/master/64_minimum-path-sum.py )
78+
7779&emsp ;&emsp ; 120 [ 三角形的最小和最大路径和] ( https://github.com/SimmyZhong/leetCode/blob/master/120_triangle.py )
You can’t perform that action at this time.
0 commit comments