Problem overview
The method canJump receives an array of hurdle heights, hurdleHeights, and the jumper's height, jumperHeight. It returns true if the jumper can clear every hurdle and false otherwise. A hurdle can be cleared when the jump height is greater than or equal to the hurdle height.
Take the heights [5, 5, 3, 4, 5] and a jump height of 3. The first hurdle is 5, which is taller than 3, so the answer is false. With [1, 2, 1] and a jump height of 1, the hurdle of height 2 is too tall, so that is false as well. With [1, 2, 3, 4, 5] and a jump height of 5, the tallest hurdle is exactly 5, which is still clearable, so the answer is true. An empty array means no hurdles, and the answer is true.
Approach: one bad hurdle is enough
Clearing all hurdles is an "all of them" condition. It fails as soon as a single hurdle fails, and it holds only if none fail. So the method hunts for a counterexample: a hurdle taller than the jumper. The first one found settles the answer as false. If the scan ends without finding one, the answer is true.
Searching for the failure, rather than counting successes, keeps the code short and lets the method stop early.
Step by step
- Loop over the hurdles with
for height in hurdleHeights. Each pass gives one hurdle height, and no index is needed. - Return
falsefor a hurdle that is too tall. The test is a strictheight > jumperHeight, because a hurdle exactly as tall as the jump can still be cleared. Thereturnends the whole method, so later hurdles are skipped. - Return
trueafter the loop. Reaching this line means no hurdle was too tall.
The solution in Swift
class Solution {
func canJump(_ hurdleHeights: [Int], _ jumperHeight: Int) -> Bool {
for height in hurdleHeights {
if height > jumperHeight {
return false
}
}
return true
}
}
Swift details worth knowing:
- Unlabeled arguments. Both parameters have a leading
_, so a call looks likeSolution().canJump([5, 4, 5, 6], 10). - An empty array is fine.
for ... inover an empty array runs zero times, so the method reachesreturn true, which is the answer the task asks for. - The standard library alternative.
hurdleHeights.allSatisfy { $0 <= jumperHeight }expresses the same idea in one line, and it also returnstruefor an empty array. The explicit loop shows what it does.
Complexity
Time: O(n) for n hurdles. In the worst case, when the jumper clears everything or only the last hurdle is too tall, each hurdle is compared once.
Space: O(1). No extra storage is used.
Edge cases and common mistakes
- Equal heights.
[1, 2, 3, 4, 5]with a jump of 5 istrue. Using>=in the test would wrongly returnfalse. - Empty array. The method returns
true. - Returning
trueinside the loop. A hurdle that can be cleared doesn't prove the rest can be. For[1, 2, 1]with a jump of 1, returningtrueat the first hurdle gives the wrong answer. - Only checking the last or first hurdle. Any hurdle in the array can be the one that fails.
Testing
[1, 2, 3, 4, 5], 5 givestrue.[5, 5, 3, 4, 5], 3 givesfalse.[5, 4, 5, 6], 10 givestrue.[1, 2, 1], 1 givesfalse.[], 0 givestrue.
