Skip to content

Complete BFS-2-1 assignment - #645

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

Complete BFS-2-1 assignment#645
tejbharath wants to merge 1 commit into
super30admin:mainfrom
tejbharath:main

Conversation

@tejbharath

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Rotting Oranges (RottenOranges.java)

Strengths:

  1. Correct implementation of the BFS algorithm for the Rotting Oranges problem.
  2. Good code organization with clear comments explaining the approach.
  3. Proper use of a queue for BFS traversal.
  4. Early return when there are no fresh oranges initially.
  5. Time and space complexity match the reference solution.

Areas for improvement:

  1. The time++ is placed after the for loop, which means it increments even when no fresh oranges are rotted in that level. This requires the time-1 adjustment at the end. Consider moving the time increment logic or adding an early return inside the BFS loop when fresh == 0 (as the reference solution does) for cleaner code.
  2. The class name RottenOranges doesn't follow Java naming conventions. It should be RottenOrangesSolution or similar, and typically LeetCode solutions use Solution as the class name.
  3. Consider adding more inline comments to explain specific logic decisions.

VERDICT: PASS


Employee Importance (EmployeeImportance.java)

Your solution is correct and well-implemented! Here are some observations:

Strengths:

  1. ✅ Correct use of HashMap for O(1) employee lookup
  2. ✅ BFS approach is appropriate for this problem
  3. ✅ Time and space complexity match the optimal solution
  4. ✅ Good comments documenting the approach and complexity
  5. ✅ Clean, readable code with descriptive variable names

Minor Improvements:

  1. The inner for (int i = 0; i < size; i++) loop with size = q.size() is unnecessary for this problem. Since we're just summing importance values, we don't need to process the queue level-by-level. You could simplify to:

    while(!q.isEmpty()){
        Employee employee = map.get(q.poll());
        importance += employee.importance;
        q.addAll(employee.subordinates);
    }

    This is more concise and equally correct.

  2. Consider using Map<Integer, Employee> interface type instead of HashMap<Integer, Employee> for better abstraction (programming to interface).

  3. The importance variable could be initialized at declaration with int importance = 0; which you already do - good!

These are minor stylistic suggestions. Your solution is functionally correct and efficient.

VERDICT: PASS

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