Skip to content

Done Graph-1#796

Open
samikshm wants to merge 1 commit into
super30admin:masterfrom
samikshm:master
Open

Done Graph-1#796
samikshm wants to merge 1 commit into
super30admin:masterfrom
samikshm:master

Conversation

@samikshm

@samikshm samikshm commented Jun 4, 2026

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Solution 1: ball_in_maze.java

  • Correctness: The DFS approach correctly handles the rolling ball mechanics by simulating movement until hitting a wall. Marking visited positions with -1 prevents infinite loops. The logic for rolling and stopping is sound.
  • Time Complexity: O(m*n) is correct since each cell is visited at most once.
  • Space Complexity: O(m*n) for the recursion stack in the worst case is accurate.
  • Code Quality: Good structure with clear separation of concerns. However, modifying the input maze (setting visited cells to -1) is a side effect that could be problematic if the maze needs to be reused. Using a separate visited array would be cleaner.
  • Edge Cases: The solution handles boundary conditions well with the while loop bounds checking. However, there's a potential issue: if start equals destination, the code returns true correctly, but if start is on a wall (value 1), the initial check would cause an ArrayIndexOutOfBoundsException before reaching the visited check.

Solution 2: town_judge.java

  • Correctness: The indegree approach is optimal and correctly identifies the town judge. The logic of decrementing for trusting and incrementing for being trusted is correct.
  • Time Complexity: O(n + m) is correct.
  • Space Complexity: O(n) is correct.
  • Code Quality: Clean, concise, and follows best practices. The solution handles the edge case of n=1 (no trust relationships) correctly, returning 1 as the judge.
  • Edge Cases: Well-handled. When trust array is empty, all indegrees remain 0, so for n=1 it returns 1 (correct), and for n>1 it returns -1 (correct).

General Observations:

  • Both solutions include good comments explaining the approach.
  • The time/space complexity analysis is accurate for both.
  • Both solutions successfully run on Leetcode as stated.

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