Minimum Path Sum
Minimum Path Sum
Given an m×n grid filled with non-negative integers, find a path from the top-left to the bottom-right that minimizes the sum of all numbers along the path. You can only move right or down at each step. Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer. Example: 3×3 grid [[1,3,1],[1,5,1],[4,2,1]] → 7 (path: 1→3→1→1→1)
Input format — only if your code reads from stdin
Input format (stdin): - First line: two integers m (rows) and n (cols) - Next m lines: each with n space-separated non-negative integers Output: a single integer — the minimum path sum.
Approach
2D DP: dp[i][j] = minimum sum to reach cell (i,j). dp[0][0] = grid[0][0]. First row: dp[0][j] = dp[0][j-1] + grid[0][j]. First column: dp[i][0] = dp[i-1][0] + grid[i][0]. Otherwise: dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1]).
Input
Output
- •
1 ≤ m, n ≤ 200, 0 ≤ grid[i][j] ≤ 100
More Dynamic Programming problems
Browse all coding problems →Press Run to test your custom input, or Submit to judge against all test cases.
Sign in to continue
Create a free account to run code, submit solutions and track progress.
- Free forever — no credit card
- 10+ languages with live execution
- Points, streaks & leaderboards