Skip to content

[getImportance && RottingOranges ]#620

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

[getImportance && RottingOranges ]#620
takchiks wants to merge 2 commits into
super30admin:mainfrom
takchiks:main

Conversation

@takchiks
Copy link
Copy Markdown

@takchiks takchiks commented Mar 5, 2026

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

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:

  • Correct use of BFS to simulate the rotting process.
  • Proper handling of directions using a direction array.
  • Good variable names and comments.

Areas for improvement:

  1. You can optimize by breaking early from the inner for-loop when fresh becomes zero. Currently, even if fresh becomes zero during the processing of a level, you continue processing the entire level. While this does not affect correctness, it can save a few operations. You can add a check inside the inner for-loop after decrementing fresh: if (fresh == 0) return count; Note that you are already incrementing count at the beginning of the level. So if during the level you rot the last fresh orange, you can return the current count immediately.

  2. The code for the Rotting Oranges problem is placed in a file that also contains code for another problem (Employee Importance). This might be a copy-paste error. Ensure that each solution is in its own file to avoid confusion.

  3. Although not necessary, you can avoid incrementing count if the current level does not rot any orange. But in your implementation, you only add to the queue when there is a fresh orange, so each level will have at least one orange to process. So it is fine.

Example of breaking early in the inner loop:

for (int j = 0; j < dir.length; j++) {
    int r = curr[0] + dir[j][0];
    int c = curr[1] + dir[j][1];
    if (r >= 0 && c >=0 && r < m && c < n && grid[r][c] == 1) {
        fresh--;
        grid[r][c] = 2;
        q.add(new int [] {r, c});
        if (fresh == 0) return count; // break early
    }
}

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.

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