Skip to content

Completed Graph-1#789

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

Completed Graph-1#789
sarvanibaru wants to merge 1 commit intosuper30admin:masterfrom
sarvanibaru:master

Conversation

@sarvanibaru
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Find the town Judge (FindJudge.java)

Your solution is excellent and correctly solves the problem. You have accurately analyzed the time and space complexity. However, note that the overall time complexity is O(E + N) because you have two loops: one over the trust array (which has E elements) and one over the n people. So while the first loop dominates when E is large, it is still important to account for both. Your comment says O(E) which might be a minor oversight. Otherwise, your code is clean and efficient. Well done!

VERDICT: PASS


Ball in the Maze (TheMaze.java)

Strengths:

  • The solution correctly implements the DFS approach with backtracking.
  • The use of a visited array prevents cycles and redundant checks.
  • The code is well-commented and the logic is explained clearly.

Areas for Improvement:

  • The main issue is that the DFS does not stop when the destination is found. You should modify the DFS to return a boolean value and use that to terminate early. For example, after setting flag = true, you should return immediately and also check flag before making recursive calls to avoid unnecessary searches.
  • Consider using an iterative BFS (like the reference solution) to avoid potential stack overflow and for better performance in terms of early termination.
  • Instead of using instance variables (like flag, dirs, m, n), it's better to keep them as local variables or pass them as parameters to make the method more self-contained and thread-safe.
  • In the DFS method, you should check if the current node is visited at the beginning to avoid redundant processing, though your current code sets visited before the loop which is correct.

Here's how you can modify the DFS to return early:

private boolean dfs(int[][] maze, int i, int j, int[] destination, boolean[][] visited) {
    if (i == destination[0] && j == destination[1]) {
        return true;
    }
    if (visited[i][j]) {
        return false;
    }
    visited[i][j] = true;
    for (int[] dir : dirs) {
        // ... same rolling logic
        if (!visited[nr][nc]) {
            if (dfs(maze, nr, nc, destination, visited)) {
                return true;
            }
        }
    }
    return false;
}

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