Jump Game VII
Real-world version: Pontoon Crossing With A Hop RangeThe statement here is our own: the same inputs, outputs and technique.
A temporary walkway across a channel is described by s, one character per pontoon along the line. '0' marks a pontoon that still floats and can be stood on, '1' marks one that has gone under and cannot.
A surveyor starts on pontoon 0, which always floats. From the pontoon at index i the surveyor may hop to the pontoon at index j when all of the following hold:
i + minJump <= j <= i + maxJump,jis within the walkway, soj <= s.length - 1,s[j]is'0'.
Hops are always forward and there is no limit on how many are taken.
Return true if the surveyor can arrive at the last pontoon, index s.length - 1, and false if no series of hops gets there.
Examples
Example 1
- Input
- s = "01000110", minJump = 2, maxJump = 3
- Output
- true
Hopping 0 -> 2 -> 4 -> 7 uses spans of 2, 2 and 3, and pontoons 2, 4 and 7 all float, so the far side is reached.
Example 2
- Input
- s = "0010", minJump = 2, maxJump = 2
- Output
- false
Every hop covers exactly two pontoons, so the only landing from pontoon 0 is pontoon 2, which is under water, and the surveyor is stuck at the start.
Example 3
- Input
- s = "0110", minJump = 3, maxJump = 3
- Output
- true
A single hop of three lands directly on pontoon 3, which floats.
Example 4
- Input
- s = "00001", minJump = 1, maxJump = 2
- Output
- false
The last pontoon is submerged, so it cannot be stood on and the crossing fails.
Constraints
- 2 <= s.length <= 10^5
- s[i] is either '0' or '1'.
- s[0] == '0'
- 1 <= minJump <= maxJump < s.length
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def can_cross_channel(deck: str, min_span: int, max_span: int) -> bool:public boolean canCrossChannel(String deck, int minSpan, int maxSpan)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 4 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Minimum Number of Flips to Make the Binary String AlternatingReal-world version: Repaint the Calibration BandMedium
- Get Equal Substrings Within BudgetReal-world version: Longest Stretch Retinted Within BudgetMedium
- Maximum Sum of 3 Non-Overlapping SubarraysReal-world version: Three Blocks on the Support RotaHard
- Find the Original Typed String IIReal-world version: Counting the Labels the Stamping Could Have MeantHard