Data Structures & Algorithms Graphs — BFS & DFS
💡
Exercise 113

Rotting Oranges 25 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

0=empty, 1=fresh, 2=rotten. Each minute, rotten oranges rot adjacent fresh ones. Find minimum minutes until no fresh remain, or -1 if impossible.

Classic multi-source BFS. Start from ALL rotten oranges simultaneously. Each BFS level = 1 minute.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Find minimum minutes for all oranges to rot. Return -1 if impossible.
Enqueue all rotten positions. BFS level by level, rotting fresh neighbors. After BFS, check if fresh == 0.
🧪 Test Cases
Input
orangesRotting([[2,1,1],[1,1,0],[0,1,1]])
Expected
4
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.