-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2304.py
18 lines (15 loc) · 837 Bytes
/
m2304.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def minPathCost(self, grid: List[List[int]], moveCost: List[List[int]]) -> int:
dp = [grid[0]] + [[inf] * len(grid[0]) for _ in range(len(grid) - 1)]
# Iterating through the rows from 0 to the one prior to last
for r in range(len(dp) - 1) :
# Iterating each column
# (r, c) = grid source indx
for c in range(len(dp[0])) :
# Iterating through the potential future spots
for nxtCol in range(len(dp[0])) :
# Calculating the new value and setting it if it's cheaper
nxtVal = dp[r][c] + moveCost[grid[r][c]][nxtCol] + grid[r + 1][nxtCol]
if dp[r + 1][nxtCol] > nxtVal :
dp[r + 1][nxtCol] = nxtVal
return min(dp[-1])