Skip to content

Commit 3f0495f

Browse files
authored
Create minimum-path-cost-in-a-grid.py
1 parent f3dd991 commit 3f0495f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Python/minimum-path-cost-in-a-grid.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time: O(m * n^2)
2+
# Space: O(n)
3+
4+
# dp
5+
class Solution(object):
6+
def minPathCost(self, grid, moveCost):
7+
"""
8+
:type grid: List[List[int]]
9+
:type moveCost: List[List[int]]
10+
:rtype: int
11+
"""
12+
dp = [[0]*len(grid[0]) for _ in xrange(2)]
13+
dp[0] = [grid[0][j] for j in xrange(len(grid[0]))]
14+
for i in xrange(len(grid)-1):
15+
for j in xrange(len(grid[0])):
16+
dp[(i+1)%2][j] = min(dp[i%2][k]+moveCost[x][j] for k, x in enumerate(grid[i]))+grid[i+1][j]
17+
return min(dp[(len(grid)-1)%2])

0 commit comments

Comments
 (0)