Loading
Loading
A warehouse stores produce in a grid of crates. Each slot is empty (0), holds fresh produce (1), or holds mouldy produce (2). Every minute, mould jumps from each mouldy crate to every fresh crate directly above, below, left or right of it. Empty slots block it.
Return the number of minutes until no fresh crate remains in crates. If some fresh crate can never be reached, return -1. If there is nothing fresh to begin with, return 0.
crates = [[2,1,1],[1,1,0],[0,1,1]]minute 1(0,1) and (1,0) turn mouldyminute 2(0,2) and (1,1)minute 3(2,1)minute 4(2,2) — nothing fresh remains4Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 2 | 1 | 1 |
|---|---|---|---|
| 1 | 1 | 1 | 0 |
| 2 | 0 | 1 | 1 |
Example 2
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(rows × cols) time · O(rows × cols) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 2 | 1 | 1 |
|---|---|---|---|
| 1 | 1 | 1 | 0 |
| 2 | 0 | 1 | 1 |
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
| 0 | 2 | 1 | 1 |
|---|---|---|---|
| 1 | 0 | 1 | 1 |
| 2 | 1 | 0 | 1 |
The fresh item in the bottom-left corner has no neighbour that can ever turn mouldy.
Example 3
| 0 | 0 | 2 |
|---|
Nothing fresh to begin with, so zero minutes are needed.