-
Notifications
You must be signed in to change notification settings - Fork 12
feat: implement BFS solution for Jump Game III (#1306) #2168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||||||||||||||||||||||||||||||||||||||
| 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() | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+5
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| while queue: | ||||||||||||||||||||||||||||||||||||||||||
| # Get the current position | ||||||||||||||||||||||||||||||||||||||||||
| curr = queue.pop(0) | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+9
|
||||||||||||||||||||||||||||||||||||||||||
| 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
AI
Feb 19, 2026
There was a problem hiding this comment.
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
AI
Feb 19, 2026
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).