-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumPathSum.py
More file actions
21 lines (16 loc) · 858 Bytes
/
MinimumPathSum.py
File metadata and controls
21 lines (16 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Question link - https://leetcode.com/problems/minimum-path-sum/description/?envType=study-plan-v2&envId=top-interview-150
class Solution:
def minPathSum(self, grid: List[List[int]]) -> int:
# Let get the dimension for the grid
ROWS , COLS = len(grid) , len(grid[0])
# Let make the grid for gthe burte approch
res = [[float("inf")] * (COLS + 1) for i in range(ROWS + 1)]
# Initalize value for the calculation
res[ROWS - 1][COLS] = 0
# Traverse the grid -> Brute force approch
for r in range(ROWS - 1, -1 , -1):
for c in range(COLS - 1, -1 ,-1):
# To get the min value, at ith index value.
res[r][c] = grid[r][c] + min(res[r + 1][c] , res[r][c + 1])
# Return the value at top left -> min path sum
return res[0][0]