Minimum Path Sum (Space Optimized)

Given a non-empty grid of non-negative integers, return the minimum path sum from the top-left to the bottom-right cell using only right and down moves. Pattern focus: Space optimization. Reduce the standard 2D DP table to a single row while preserving correctness.

Input Format

grid is a non-empty matrix of non-negative integers

Output Format

minimum path sum

Constraints

  • 1 <= rows, cols <= 200

Examples

Example 1:

Input:

grid = [[1,3,1],[1,5,1],[4,2,1]]

Output:

7

Explanation:

The minimum cost remains 7 even when solved with a rolling array.

Example 2:

Input:

grid = [[1,2,3],[4,5,6]]

Output:

12

Explanation:

The minimum sum is 12.

Example 3:

Input:

grid = [[5]]

Output:

5

Explanation:

A single cell still returns 5.

Loading...
Minimum Path Sum (Space Optimized) - Dp Grid