Dodomeki
Sign in

76. Rotting Oranges

Medium

A grid contains empty cells (0), fresh oranges (1), and rotten oranges (2). Every minute, any fresh orange adjacent by side to a rotten orange also becomes rotten. Return how many minutes are needed until no fresh orange remains, or -1 if this is impossible.

Example 1
Input: grade = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
Example 2
Input: grade = [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Constraints
  • 1 <= linhas, colunas <= 10
  • The grid contains only 0, 1, and 2.
GraphsBFSMatrixQueue
Caso 1
grade =
[[2,1,1],[1,1,0],[0,1,1]]
Expected =
4
Caso 2
grade =
[[2,1,1],[0,1,1],[1,0,1]]
Expected =
-1

Run checks these 2 cases. Submit runs all 6, including hidden ones.