You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Your solution for the "Rotting Oranges" problem has the right idea (using BFS) but contains a few critical bugs and missing checks.
Strengths:
You correctly identified the BFS approach for simulating the rotting process.
You used a queue to process the rotten oranges level by level.
You maintained a count of fresh oranges to check if all become rotten.
Areas for improvement:
Boundary Check: In the condition for checking the next cell, you have c < n but it should be c < m because m is the number of columns. This is because you defined n = grid.length (rows) and m = grid[0].length (columns). So the correct condition should be: if (r >= 0 && c >= 0 && r < n && c < m && grid[r][c] == 1)
This error might cause an ArrayIndexOutOfBoundsException.
Handling No Fresh Oranges Initially: You should check if freshCount is zero before starting the BFS and return 0 immediately. This handles the case when there are no fresh oranges at minute 0. Currently, your code does not handle the case when there are no rotten oranges and no fresh oranges: it would return -1 (because after the while loop, time is 0, and you return time-1 which is -1) but it should return 0.
Early Termination: During the BFS, when you rot an orange, you decrement freshCount. You should check immediately after decrementing if freshCount becomes 0. If it does, you can return the current time + 1? But note: you haven't incremented time for the current level yet. Actually, you are processing a level. The time for the current level is the current value of time (which is the time for the previous level) plus 1. However, you increment time at the end of the level. So if you are processing the first level (which is the initial rotten oranges), time is 0. Then you process the neighbors and for each fresh orange you rot, you decrement freshCount. If freshCount becomes 0 during this level, then the minutes required is 1. But after processing this level, you increment time to 1. So you should return time (which will be 1) immediately when freshCount becomes 0. However, you cannot return inside the inner for-loop because you are iterating over the directions. Instead, you should check after processing all directions for one orange? Actually, you should check after each orange you rot. So you can break early.
The reference solution does:
for (each neighbor) {
if (fresh orange) {
rot it;
fresh--;
if (fresh==0) return time;
}
}
But note: the reference solution increments time at the beginning of the while loop (so the first level returns time=1). However, the reference solution initializes time=0 and then if there are no fresh, returns 0. Then in the while loop, it sets time++ at the start. So for the first level, time becomes 1.
In your code, you have:
int time = 0;
while (!q.isEmpty()) {
int size = q.size();
time++; // you increment here -> so for the
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.