Skip to content

Adding all the solutions#616

Open
subbu4061 wants to merge 1 commit into
super30admin:mainfrom
subbu4061:main
Open

Adding all the solutions#616
subbu4061 wants to merge 1 commit into
super30admin:mainfrom
subbu4061:main

Conversation

@subbu4061
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Your solution for the "Oranges Getting Rotten" problem is correct and efficient. Well done! Here are some suggestions for improvement:

  1. Variable Naming: Consider renaming oneCount to freshCount to make it clearer that it represents the number of fresh oranges.
  2. Direction Loop: Instead of using an index l to iterate over directions, you can use a for-each loop for better readability. For example:
    for (int[] dir : dirs) { ... }
  3. Early Termination: You can check if freshCount becomes zero during the BFS and return immediately. This would avoid processing unnecessary levels. For example, inside the inner loop when you rot a fresh orange, you can check if freshCount == 0 and return time (but note: in your current code, you increment time after processing each level. So if you break early, you need to return the current time without incrementing again). Actually, in your code, the time variable is incremented at the end of the level. So if you rot the last fresh orange during the current level, then after processing the entire level, you increment time. But that would be one extra increment. However, you are returning time-1 at the end. So to break early, you should return `time

@super30admin
Copy link
Copy Markdown
Owner

Your solution is correct and efficient for the problem constraints. However, there are a few improvements you can make:

  1. Early termination: You can check during the BFS if the fresh count becomes zero and return immediately. This would avoid processing unnecessary levels. For example, inside the inner loop when you rot a fresh orange, you can check if fresh becomes zero and return time (without waiting for the entire level to finish). But note: you are incrementing time at the end of the level. So if you break during the level, you must return the current time (which is the time for the next minute). Actually, in your code, the time for the current level is being processed, and the time variable has not been incremented for this level yet. So if you rot the last fresh orange during the processing of the current level, the minute for that level is not yet completed? Actually, the time variable is incremented after processing all elements in the current level. So if you break during the level, you should return time+1? Not exactly. Let me explain: in your

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