Minimum Path Sum with Obstacles

Given a grid of positive costs where -1 marks a blocked cell, return the minimum total cost to move from the top-left cell to the bottom-right cell using only right and down moves. If the destination is unreachable, return -1. Pattern focus: Obstacles. Combine shortest-path style reasoning with DP state propagation.

Input Format

grid contains positive costs and -1 for blocked cells

Output Format

minimum reachable cost or -1

Constraints

  • 1 <= rows, cols <= 200

Examples

Example 1:

Input:

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

Output:

11

Explanation:

The blocked center forces the cheapest route to total 11.

Example 2:

Input:

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

Output:

10

Explanation:

The best reachable route has cost 10.

Example 3:

Input:

grid = [[1,2,3],[1,-1,1],[1,1,1]]

Output:

5

Explanation:

The minimum cost path avoids the blocked middle cell and totals 5.

Loading...
Minimum Path Sum with Obstacles - Dp Grid