Skip to content

solution to BFS 1#1638

Open
shakthinandana wants to merge 1 commit intosuper30admin:masterfrom
shakthinandana:master
Open

solution to BFS 1#1638
shakthinandana wants to merge 1 commit intosuper30admin:masterfrom
shakthinandana:master

Conversation

@shakthinandana
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Level Order Traversal in Binary tree (level-order-traversal.py)

Your solution is well-written and correct. Here are some points to consider for improvement:

  • Instead of initializing res with an empty list and then appending an empty list for each level at the start of the while loop, you can create the sublist for the current level inside the for loop and append it to res after the for loop. This might be slightly more efficient and avoids having to index res with level. For example:

    while queue:
        level_elements = len(queue)
        temp = []
        for i in range(level_elements):
            popped = queue.popleft()
            temp.append(popped.val)
            # ... rest of the code
        res.append(temp)
        level += 1  # if you still need level for other purposes, but otherwise it's not necessary

    However, note that in this problem, we don't need the level variable at all for the result. The above change would make the code cleaner.

  • The variable name level_elements might be better named as level_size or nodes_in_level to more clearly indicate that it represents the number of nodes at the current level.

  • You included a comment about the code running on Leetcode, which is good. However, it's also helpful to add docstrings or comments explaining the approach briefly.

Overall, your solution is excellent. Keep up the good work!

VERDICT: PASS


Scheduling Courses (course-schedule.py)

Strengths:

  • The solution correctly implements Kahn's algorithm for topological sorting.
  • The time and space complexity are optimal.
  • The code is clean and well-commented, with clear variable names.

Areas for improvement:

  1. The condition if c==numCourses: return True inside the while loop is efficient but can be moved outside the loop to avoid checking in every iteration. The algorithm will naturally terminate when the queue is empty, and then we can check if the count equals the number of courses.
  2. The initial check for if c==numCourses: return True is correct but might be redundant if there are no prerequisites. However, it is a good optimization.
  3. The variable c is initialized to count the number of courses with zero indegree. Then, during processing, it increments when a course's indegree becomes zero. This is correct.
  4. The variable name ndeg could be more descriptive, such as indegree.
  5. The solution uses defaultdict(list) for the adjacency list, which is appropriate.

Suggested improvements:

  • Rename ndeg to indegree for clarity.
  • Instead of checking if c==numCourses inside the while loop for every neighbor, you can check after the while loop whether c == numCourses. This would be more efficient as it avoids repeated checks.
  • Alternatively, you can keep the check inside the loop for early termination, which is acceptable.

Revised code snippet for clarity and efficiency:

from collections import defaultdict, deque

class Solution(object):
    def canFinish(self, numCourses, prerequisites):
        indegree = [0] * numCourses
        adjlist = defaultdict(list)
        count = 0
        queue = deque()

        for pr in prerequisites:
            indegree[pr[0]] += 1
            adjlist[pr[1]].append(pr[0])

        for i in range(numCourses):
            if indegree[i] == 0:
                count += 1
                queue.append(i)

        if count == numCourses:
            return True

        while queue:
            node = queue.popleft()
            for neighbor in adjlist[node]:
                indegree[neighbor] -= 1
                if indegree[neighbor] == 0:
                    count += 1
                    queue.append(neighbor)
                    # Early termination: if we have processed all courses, return
                    if count == numCourses:
                        return True

        return count == numCourses

However, the current solution is correct and efficient. The early termination inside the loop is a good optimization.

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