InferenceAI

Enter at least two characters to search.

Start free

Spoiling Fruit

mediumGraphs25 min25 XP

In the grid, 0 is empty, 1 is fresh fruit and 2 is spoiled fruit. Every minute, fresh fruit next to spoiled fruit (up, down, left, right) spoils. Return the minutes until no fresh fruit remains, or -1 if some never spoils.

Examples

EXAMPLE WALKTHROUGHfour minutes

A guided view of the published example, not a trace of your code.

INPUT[[[2,1,1],[1,1,0],[0,1,1]]]
TASKspoiling_fruit()
EXPECTED OUTPUT4

Start with the input values shown in this example.

Example 1 of 3

Input: grid = [[2,1,1],[1,1,0],[0,1,1]]

Output: 4

Explanation: four minutes

Input: grid = [[2,1,1],[0,1,1],[1,0,1]]

Output: -1

Explanation: unreachable

Input: grid = [[0,2]]

Output: 0

Explanation: nothing fresh

Hint 1

Multi-source BFS starting from every spoiled fruit at once.

Topics

DSAGraphsPython