Done BFS-2-1#627
Conversation
Oranges getting rotten (RottingOranges.java)Your solution has a good structure and uses BFS correctly in terms of processing levels. However, there are a few critical issues:
Here's how you can correct your solution:
Revised approach (similar to reference): int time = 0;
if (countOfFreshOranges == 0) return 0;
while (!indicesOfRottenOranges.isEmpty()) {
int size = indicesOfRottenOranges.size();
time++; // we are going to process the next level
for (int i = 0; i < size; i++) {
int[] curr = indicesOfRottenOranges.poll();
for (int[] dir : DIRECTIONS) {
int r = curr[0] + dir[0];
int c = curr[1] + dir[1];
if (r >= 0 && c >= 0 && r < numOfRows && c < numOfColumns && grid[r][c] == FRESH) {
grid[r][c] = ROTTEN;
indicesOfRottenOranges.offer(new int[]{r, c});
countOfFreshOranges--;
if (countOfFreshOranges == 0) return time;
}
}
}
}
return -1;But note: in the above, if there are no rotten oranges initially, the while loop won't run, so we return -1. However, we already checked if there are no fresh oranges, so if we have fresh oranges and the queue is empty (no rotten), we should return -1. So, after the initial for-loops, if VERDICT: NEEDS_IMPROVEMENT Importance of Employee (EmployeeImportance.java)Now, evaluate the student's solution. VERDICT: NEEDS_IMPROVEMENT |
No description provided.