data-structures · intermediate · ~15 min

Minimum path sum

Find the cheapest right/down path through a grid.

Challenge

Implement:

int min_path_sum(const int *grid, int rows, int cols);

Return the minimum sum of a path from top-left to bottom-right of a rows x cols grid (row-major, non-negative), moving only right or down.

Input format

Row-major grid, dimensions.

Output format

Minimum path sum.

Constraints

First row/column accumulate left-to-right / top-to-bottom.

Starter code

#include <stddef.h>
/* Minimum sum of a path (right/down only) from top-left to bottom-right of a rows x cols grid (row-major, non-negative). */
int min_path_sum(const int *grid,int rows,int cols){ (void)grid;(void)rows;(void)cols; return 0; }

Common mistakes

Mixing up row-major indexing (grid[i*cols+j]).

Edge cases to handle

1x1 grid -> its single cell.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.