Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Rotting Oranges

Medium

Topics
GraphBFSMatrix

In an m x n grid each cell is 0 (empty), 1 (fresh orange), or 2 (rotten). Every minute, a fresh orange adjacent (4-directionally) to a rotten one becomes rotten. Return the minutes until no fresh orange remains, or -1 if impossible.

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 <= m, n <= 10
  • grid[i][j] is 0, 1, or 2.
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access