Skip to content

Problem1 and Problem2 added#790

Open
megharaykar wants to merge 3 commits intosuper30admin:masterfrom
megharaykar:master
Open

Problem1 and Problem2 added#790
megharaykar wants to merge 3 commits intosuper30admin:masterfrom
megharaykar:master

Conversation

@megharaykar
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Find the town Judge (Problem1.py)

Your solution is excellent and matches the reference solution in both approach and complexity. You have correctly handled the indegree counting by decrementing for the trust giver and incrementing for the trust receiver. The edge case for n=1 is appropriately handled. The code is well-structured and readable with clear comments.

One minor improvement: Instead of iterating over the indices of the trust list with for i in range(len(trust)), you can use a more Pythonic direct iteration over the elements: for tr in trust. This avoids the index lookup and makes the code slightly more efficient and readable.

Also, note that the indegrees array is of length n+1, and you are iterating from index 0 to n. However, since person labels start from 1, the first element (index 0) is unused. Your loop for finding the judge starts from index 0, which is acceptable because index 0 will never have n-1 (since n>=1 and indegrees[0] is initialized to 0). But it's slightly inefficient to check index 0. You can start the loop from 1 to n: for i in range(1, n+1).

Here's the slightly optimized version:

class Solution:
    def findJudge(self, n: int, trust: List[List[int]]) -> int:
        indegrees = [0] * (n+1)
        
        for tr in trust:
            indegrees[tr[0]] -= 1
            indegrees[tr[1]] += 1
            
        for i in range(1, n+1):
            if indegrees[i] == n-1:
                return i
                
        return -1

Overall, great job!

VERDICT: PASS


Ball in the Maze (Problem2.py)

Strengths:

  • You have implemented both BFS and DFS solutions, showing a good understanding of different traversal methods.
  • The BFS solution is correct and follows the standard approach.
  • The code is well-commented and structured.

Areas for Improvement:

  1. Avoid using dir as a variable name because it is a built-in function in Python. Consider using d or direction instead.
  2. The DFS solution has a critical error: in the while loop condition, you should check for walls (1) instead of visited nodes (-1). The condition should be while r >=0 and c >=0 and r < self.m and c < self.n and maze[r][c] != 1 (similar to the BFS solution). Also, note that the ball should not stop at visited nodes; it should roll through them until hitting a wall.
  3. The DFS solution might be less efficient in practice due to recursion depth, especially for large grids. However, it is acceptable if implemented correctly. But given the error, it needs correction.
  4. It's good practice to include both solutions, but ensure that both are correct. You might want to test the DFS solution with the provided examples to catch the error.

Recommendation:

  • Stick to the BFS solution as it is more straightforward for this problem and avoids potential recursion depth issues.
  • If you prefer DFS, correct the while loop condition and ensure that the visited marking is done correctly.

VERDICT: NEEDS_IMPROVEMENT

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