-
Notifications
You must be signed in to change notification settings - Fork 0
/
64.py
38 lines (28 loc) · 832 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
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
n = len(grid)
if n == 0:
return 0
m = len(grid[0])
l1 = []
total = 0
for i in xrange(0, m):
total += grid[0][i]
l1.append(total)
l2 = list(l1)
for i in xrange(1, n):
l2[0] = l1[0] + grid[i][0]
for j in xrange(1, m):
l2[j] = min(l2[j-1]+grid[i][j], l1[j]+grid[i][j])
l1 = list(l2)
return l2[m-1]
if __name__ == "__main__":
# print Solution().minPathSum([[3, 2, 1],[5, 4, 3]])
print Solution().minPathSum([[1,2],[1,1]])