-
Notifications
You must be signed in to change notification settings - Fork 613
/
64.py
30 lines (24 loc) · 959 Bytes
/
64.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'''
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.
'''
class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if not grid:
return 0
row, col = len(grid), len(grid[0])
dp = [[0 for _ in range(col)] for _ in range(row)]
dp[0][0] = grid[0][0]
for index in range(1, row):
dp[index][0] = dp[index-1][0] + grid[index][0]
for index in range(1, col):
dp[0][index] = dp[0][index-1] + grid[0][index]
print dp
for index_i in range(1, row):
for index_j in range(1, col):
dp[index_i][index_j] = min(dp[index_i-1][index_j], dp[index_i][index_j-1]) + grid[index_i][index_j]
return dp[row-1][col-1]