Why it works
- All rotten oranges start in the queue together, so the waves spread from every one of them at the same pace.
- One round takes exactly the cells that were in the queue at its start: those rotted in the previous minute, so a round is one minute.
- Each fresh orange is counted down once, when it first rots. When fresh hits 0 the loop stops; if the queue runs dry first, some orange is cut off and the answer is -1.
Complexity
Time O(rows · cols): each cell enters the queue at most once and checks four neighbours. Memory O(rows · cols) for the queue.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗