Lv.2bfsgraphgrid
Mold Spread Time
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
A floor is a grid where 1 is moldy, 0 is clean and -1 is a wall. Every minute mold spreads to clean cells sharing an edge
with a moldy cell. Return the minimum number of minutes until no clean cell remains, -1 if some clean cell is never
reached, or 0 if there are no clean cells to begin with.
Examples
| grid | result |
|---|---|
[[1, 0, 0], [0, -1, 0], [0, 0, 0]] | 4 |
[[1, -1, 0], [0, -1, 0]] | -1 |
[[1, 0, 1], [0, 0, 0]] | 2 |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(grid: int[][]) → int
Constraints
- ,
gridis rectangular grid[i][j]is-1,0or1
Expected complexity
- Time
- O(HW)
- Space
- O(HW)
Examples
| grid | Expected |
|---|---|
[[1,0,0],[0,-1,0],[0,0,0]] | 4 |
[[1,-1,0],[0,-1,0]] | -1 |
[[1,0,1],[0,0,0]] | 2 |
Hint
Hint
Run one BFS with every moldy cell in the initial queue at distance 0 (multi-source BFS); the answer is the largest distance, or
-1 if a clean cell stays unvisited. Re-scanning the whole grid every minute is , and a winding corridor makes
.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1062 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 11 | verdict only |
| performance | 60 | 4 | verdict only |