Skip to content

Commit 75a3c4a

Browse files
committed
minimum path sum
1 parent c53c73c commit 75a3c4a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

leetcode/minimum_path_sum.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# https://leetcode.com/problems/minimum-path-sum/submissions/
2+
3+
from functools import cache
4+
5+
6+
class Solution:
7+
def minPathSum(self, grid: list[list[int]]) -> int:
8+
m, n = len(grid), len(grid[0])
9+
10+
@cache
11+
def min_sum(r: int, c: int) -> int:
12+
if r < 0 or c < 0:
13+
return float("inf")
14+
if r == 0 and c == 0:
15+
return grid[r][c]
16+
return min(min_sum(r - 1, c), min_sum(r, c - 1)) + grid[r][c]
17+
18+
return min_sum(m - 1, n - 1)

0 commit comments

Comments
 (0)