← 返回 uber 的题目列表Rotting Oranges
类型:qbank
Given an m x n grid of empty cells, fresh oranges, and rotten oranges, return the minimum number of minutes for all fresh oranges to rot, where each minute rot spreads to the four-directionally adjacent fresh oranges. Return -1 if some fresh orange can never rot.
Rotting Oranges
Given an m x n grid of empty cells, fresh oranges, and rotten oranges, return the minimum number of minutes for all fresh oranges to rot, where each minute rot spreads to the four-directionally adjacent fresh oranges. Return -1 if some fresh orange can never rot.
SWE
bfs
grid
simulation
medium
Frequency
Single report
Last asked
2026-04-13
Stage
phone-screen
Rotting Oranges
You are given an m x n grid where 0 is empty, 1 is a fresh orange, and 2 is a rotten orange.
Every minute, any fresh orange adjacent in the four cardinal directions to a rotten orange also becomes rotten. Return the minimum number of minutes needed to rot all oranges, or -1 if that is impossible.
Examples
Example 1:
Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
Example 2:
Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Constraints
1 <= grid.length, grid[i].length <= 10
grid[i][j] is 0, 1, or 2.