Skip to content

Commit 2285d71

Browse files
committed
feat: minimum-path-sum challenge
1 parent e136075 commit 2285d71

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Challenge: Given a matrix of integers, determine the minimum sum to reach from the top-left corner to the bottom-right corner, moving only right or down. Use dynamic programming to optimize the solution.
2+
3+
from min_path_sum import min_path_sum
4+
5+
def main():
6+
grid = [
7+
[1, 3, 1],
8+
[1, 5, 1],
9+
[4, 2, 1]
10+
]
11+
result = min_path_sum(grid)
12+
print(f"Minimum path sum: {result}")
13+
14+
if __name__ == "__main__":
15+
main()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def min_path_sum(grid):
2+
if not grid or not grid[0]:
3+
return 0
4+
5+
m, n = len(grid), len(grid[0])
6+
dp = [[0] * n for _ in range(m)]
7+
8+
dp[0][0] = grid[0][0]
9+
10+
for i in range(1, m):
11+
dp[i][0] = dp[i-1][0] + grid[i][0]
12+
13+
for j in range(1, n):
14+
dp[0][j] = dp[0][j-1] + grid[0][j]
15+
16+
for i in range(1, m):
17+
for j in range(1, n):
18+
dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])
19+
20+
return dp[m-1][n-1]

0 commit comments

Comments
 (0)