[getImportance && RottingOranges ]#620
Conversation
|
Your solution for the "Rotting Oranges" problem is well-implemented and follows the BFS approach correctly. You have correctly counted the fresh oranges and used a queue to process the rotten oranges. The code is readable and efficient. Strengths:
Areas for improvement:
Example of breaking early in the inner loop: But note: if you break early inside the inner for-loop, you might not process all the neighbors of the current rotten orange. However, since the goal is to return the minute when the last fresh orange becomes rotten, it is safe to return immediately when fresh becomes zero. The rest of the processing won't change the outcome. However, if you break early in the inner for-loop, you are returning in the middle of processing a level. So you might not process all the oranges in the current level. But that is acceptable because there are no more fresh oranges to rot. Alternatively, you can break out of the outer for-loop as well. But the code is simpler with the early return. Overall, your solution is correct and efficient. The minor optimization is not critical. |
No description provided.