Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions exercises/1000_programs/medium/1306_jump_game_iii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def canReach(arr: list[int], start: int) -> bool:
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description says "Closes #1306", but the referenced issue #1306 is about screenshot automation (exercises/automation/098_screenshot_auto.py), while this change adds a Jump Game III solution. Please update the PR description/linked issue so the PR closes the correct issue (or remove the close directive).

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function name canReach doesn't follow the repo naming convention for functions/variables (lowercase_with_underscores). Consider renaming to can_reach (and updating the call sites in this file).

Copilot uses AI. Check for mistakes.
# 1. Initialize the queue with our starting position
queue = [start]
# 2. Keep track of visited indices so we don't get stuck in a loop
visited = set()
Comment on lines +1 to +5
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is missing a docstring. CONTRIBUTING.md specifies adding docstrings for functions/classes; please add a short docstring describing parameters, return value, and the BFS approach.

Copilot uses AI. Check for mistakes.

while queue:
# Get the current position
curr = queue.pop(0)
Comment on lines +1 to +9
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queue.pop(0) makes the BFS dequeue operation O(n) per pop, which can degrade to O(n^2). Use collections.deque and popleft() (and ideally initialize the queue as a deque) to keep the BFS truly O(n).

Suggested change
def canReach(arr: list[int], start: int) -> bool:
# 1. Initialize the queue with our starting position
queue = [start]
# 2. Keep track of visited indices so we don't get stuck in a loop
visited = set()
while queue:
# Get the current position
curr = queue.pop(0)
from collections import deque
def canReach(arr: list[int], start: int) -> bool:
# 1. Initialize the queue with our starting position
queue = deque([start])
# 2. Keep track of visited indices so we don't get stuck in a loop
visited = set()
while queue:
# Get the current position
curr = queue.popleft()

Copilot uses AI. Check for mistakes.

# SUCCESS: We found a zero!
if arr[curr] == 0:
return True
Comment on lines +1 to +13
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no input validation for arr being empty or start being out of bounds; arr[curr] will raise IndexError in those cases. Add an early guard (e.g., return False or raise a clear ValueError) before accessing arr[start].

Copilot uses AI. Check for mistakes.

# Mark current as visited
visited.add(curr)

# Calculate possible jumps
for next_pos in [curr + arr[curr], curr - arr[curr]]:
# Check if the jump is within the array bounds and not visited
if 0 <= next_pos < len(arr) and next_pos not in visited:
queue.append(next_pos)
Comment on lines +7 to +22
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visited bookkeeping happens only after dequeuing, which can allow the same index to be enqueued multiple times before it's processed. Mark nodes visited when enqueuing (or skip processing if curr is already visited right after dequeue) to avoid duplicate work.

Copilot uses AI. Check for mistakes.

# If the queue is empty and we never hit 0, it's impossible
return False

# --- Test Cases ---
if __name__ == "__main__":
# Test 1: Should be True (can reach index 5 or 6)
print(f"Test 1: {canReach([4,2,3,0,3,1,2], 5)}")

# Test 2: Should be True (start is already 0)
print(f"Test 2: {canReach([4,2,3,0,3,1,2], 0)}")

# Test 3: Should be False (cannot reach 0)
print(f"Test 3: {canReach([3,0,2,1,2], 2)}")
Loading