BFS 2-1 Done#629
Conversation
Oranges getting rotten (Problem1.py)Your solution is mostly correct and follows the BFS approach. Here are some points for improvement:
Actually, the reference solution breaks early inside the inner loop: when it rots an orange and fresh becomes 0, it returns the current time (which is the minute for the current level). In your code, you don't have a variable "fresh" that tracks the number of fresh oranges. Instead, you track rottenOrangesCount and totalOranges. So you can do the same: when you rot an orange, check if rottenOrangesCount == totalOranges. Then, you can return resultMin+1? But note: at the beginning of the while loop, resultMin is the number of minutes that have passed. The current level you are processing will cause the next minute? Actually, no: in your code, you are processing the current level (which corresponds to minute resultMin). Then, when you rot an orange in this level, it is happening at minute resultMin. So if you rot the last orange in this level, then the answer should be resultMin. But wait: how do you define the minutes? The initial state is minute 0. Then, after one minute, the first level of rotting occurs. So if we are processing the first level (initial rotten oranges), then resultMin is 0. Then, when we process their neighbors, we are simulating the first minute. So if we rot the last orange during the first minute, we should return 1. But in VERDICT: NEEDS_IMPROVEMENT Importance of Employee (Problem2.py)Your solution is correct and efficient, and you have successfully implemented a BFS approach to traverse the hierarchy. Here are some suggestions for improvement:
Here's a revised version of your code with these improvements: from collections import deque
class Solution:
def getImportance(self, employees: List['Employee'], id: int) -> int:
employee_map = {}
for employee in employees:
employee_map[employee.id] = employee
queue = deque([id])
total_importance = 0
while queue:
emp_id = queue.popleft()
emp = employee_map[emp_id]
total_importance += emp.importance
for sub_id in emp.subordinates:
queue.append(sub_id)
return total_importanceThis version is more concise and avoids redundant storage. Overall, your solution is strong and demonstrates a good understanding of the problem. Keep up the good work! VERDICT: PASS |
No description provided.