Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Graphs: representation

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Graphs: representation

Rotting Oranges

intermediatelayer I4 · Graphs: representation5 tests

Each cell of grid is 0 (empty), 1 (a fresh orange) or 2 (a rotten orange). Every minute, a rotten orange rots each fresh orange directly above, below, left or right of it.

Return the number of minutes until no fresh orange remains, or -1 if some fresh orange can never rot.

  • All rotten oranges spread simultaneously, so this is a multi-source breadth-first search — not one BFS per rotten cell
  • If there are no fresh oranges to begin with, the answer is 0

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen