BugForge
DSA

Spoiling Packages

Mediummulti-source BFSgrid 00:00

Grid cells: 0 empty, 1 fresh package, 2 spoiled. Every minute, spoilage spreads from each spoiled package to fresh 4-directional neighbours. Return the minutes until no fresh package remains, or -1 if some can never spoil.

Signature

minutesToSpoil(grid: int[][]) → int
Example 1
grid = [[2,1,1],[1,1,0],[0,1,1]]
output = 4
4 minutes
Example 2
grid = [[2,1,1],[0,1,1],[1,0,1]]
output = -1
bottom-left is unreachable → -1
Example 3
grid = [[0,2]]
output = 0
no fresh packages → 0

Constraints

  • 1 ≤ rows, cols ≤ 200
Run checks the examples. Submit runs every hidden test (18, including large inputs that catch slow solutions).