2dbi

Solve the Rotten Oranges problem

via2dbi

Problem

Rotten Oranges: in a grid of fresh (1), rotten (2), and empty (0) cells, each minute every rotten orange rots its 4-directional fresh neighbours. Return the minutes until no fresh orange remains, or -1 if impossible.

Example

[[2,1,1],
 [1,1,0],
 [0,1,1]] -> 4

Approach

Multi-source BFS starting from all rotten oranges at once; the number of BFS levels is the answer.

Add a follow-up question they asked
Why multi-source BFS?
Detect unreachable fresh oranges
asked …
LeaderboardSalary
Language
Account