Skip to content
Decoder

Rotting Oranges

Medium

Problem

A crate is a grid where 0 is an empty slot, 1 is a fresh orange and 2 is a rotten one. Every minute, each rotten orange rots the fresh oranges directly above, below, left and right of it. Return the number of minutes until no fresh orange is left, or -1 if some fresh orange can never be reached.

Example 1

[[2, 1, 1],

Example 2

 [1, 1, 0],

Example 3

Input
[0, 1, 1]]
Output
4

Example 4

[[2, 1, 1],

Example 5

 [0, 1, 1],

Example 6

Input
[1, 0, 1]]
Output
-1
Why
bottom-left orange is cut off

Example 7

Input
[[0, 2]]
Output
0
Why
nothing fresh to begin with

Constraints

  • 1 <= grid.length, grid[i].length <= 10
  • grid[i][j] is 0, 1 or 2

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.