Skip to content

Done BFS-2-1#627

Open
rvuyyuru7 wants to merge 2 commits into
super30admin:mainfrom
rvuyyuru7:main
Open

Done BFS-2-1#627
rvuyyuru7 wants to merge 2 commits into
super30admin:mainfrom
rvuyyuru7:main

Conversation

@rvuyyuru7
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

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:

  • The variable countOfNewlyRottenOranges is used to accumulate the total number of oranges rotten during the entire process. This is incorrect because you need to check when the initial number of fresh oranges becomes zero. Instead, you should decrement the countOfFreshOranges each time you rot an orange and check if it becomes zero. This is more efficient and correct.

  • Also, consider the case where there are no rotten oranges initially. If there are fresh oranges, the queue will be empty, so the while loop won't run, and you'll return 0 (which is incorrect). You should return -1 in that case. However, in your current code, if there are no rotten oranges and there are fresh oranges, the initial check for countOfFreshOranges == 0 returns 0, which is wrong. Actually, you should return -1 if there are fresh oranges but no way to rot them (i.e., no rotten oranges to start with). So, after initializing the queue, if countOfFreshOranges > 0 and the queue is empty, you should return -1 immediately.

  • Another minor point: you increment minNumOfMinutes at the end of each level. However, the first level of rotten oranges (the initial ones) should be at time 0. So, you should start with minNumOfMinutes = -1 and then increment at the beginning of the while loop, or adjust the timing. The reference solution handles this by starting time at 0 and then incrementing at the start of the while loop, but only if there are fresh oranges. Alternatively, you can return minNumOfMinutes - 1 if you start at 0 and increment each level.

Here's how you can correct your solution:

  1. Remove countOfNewlyRottenOranges and instead use countOfFreshOranges by decrementing it each time you rot an orange.
  2. After the initial for-loops, if countOfFreshOranges == 0, return 0.
  3. If the queue is empty and there are fresh oranges (i.e., countOfFreshOranges > 0), return -1.
  4. Start with minNumOfMinutes = 0 and then in the while loop, after processing each level, increment the time. But note: the initial rotten oranges are at time 0. So, the first time you process neighbors, it should be for time 1. So, you can set minNumOfMinutes = -1 initially and then increment at the start of the while loop. Or, as in the reference solution, start at 0 and increment at the start of the while loop, but then return time when fresh becomes 0.

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 countOfFreshOranges > 0 and the queue is empty, return -1. Actually, the reference solution does not have this explicit check because if there are no rotten oranges, the queue is empty, so the while loop doesn't run, and then it returns -1 at the end. So, the above code is correct.

VERDICT: NEEDS_IMPROVEMENT


Importance of Employee (EmployeeImportance.java)

Now, evaluate the student's solution.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants