Problem overview
The method canJump receives an array of hurdle heights, hurdleHeights, and the height the jumper can clear, jumperHeight. It returns true if the jumper can clear every hurdle and false if at least one hurdle is too tall. A hurdle is cleared when the jump height is greater than or equal to the hurdle height, so a hurdle exactly as tall as the jump still counts as cleared.
Take hurdles [1, 2, 3, 4, 5] and a jump of 5. The tallest hurdle is 5, which is equal to the jump, so every hurdle is cleared and the answer is true. With hurdles [5, 5, 3, 4, 5] and a jump of 3, the first hurdle is already too tall, so the answer is false. With [1, 2, 1] and a jump of 1, the hurdle of height 2 is too tall, so the answer is false even though the other two are fine.
An empty array of hurdles must give true: with no hurdles in the way, any jump height gets through.
Approach: one bad hurdle is enough
The question is "can the jumper clear all of them?". That phrasing leads to a pattern. A single hurdle that is too tall settles the answer: it is false, and there is no point in looking further. On the other hand, a hurdle that is low enough proves nothing about the rest. The answer can only be true after every hurdle has passed the test.
So the method visits the hurdles one by one. If any of them is taller than the jumper can clear, it returns false immediately. If the loop ends without that happening, every hurdle was low enough, and the method returns true.
It is easier to test the failing case than the passing one. "Is this hurdle too tall?" is one strict comparison, while the passing case would be the opposite, and you would have to combine the results of all the hurdles. Stopping at the first failure avoids that.
Step by step
- Loop over the heights. The
for...ofloop gives the elements ofhurdleHeightsdirectly. The loop variable is calledheight, and each pass holds the height of one hurdle. - Return
falseon a hurdle that is too tall. The test isheight > jumperHeight. It is a strict greater-than because equal heights are allowed: the jumper clears a hurdle of height 5 with a jump of 5. Using>=would wrongly reject that case. Thereturnends the method at once, so the remaining hurdles are skipped. - Return
trueafter the loop. The line is reached only if no hurdle triggered the early return. That means every hurdle was cleared. For an empty array the loop does not run at all, so the method returnstrue, exactly as the problem demands, with no special case written.
The solution in TypeScript
class Solution {
canJump(hurdleHeights: number[], jumperHeight: number): boolean {
for (const height of hurdleHeights) {
if (height > jumperHeight) {
return false;
}
}
return true;
}}
A few details on this code:
- The method is called
canJump. The examples in the task call ithurdleJump, but the editor and the lesson's tests usecanJumpinside the classSolution, so that is the name to keep. for...ofversusfor...in.for (const height of hurdleHeights)gives numbers.for...ingives the indices as strings, and comparing a string index with a number would silently check the wrong thing.- Closing braces on one line. The reference puts the class's final
}right after the method's, as}}. It is only a formatting choice and does not change the meaning. - The annotations disappear at run time.
number[]andbooleanare checked by the compiler and erased from the JavaScript. The loop itself only relies on the comparison. - Comparisons with
NaNare always false. A hurdle height ofNaNwould never be greater than the jump, so it would never fail the check. That is not something the lesson's inputs contain.
Complexity
Time: O(n), where n is the number of hurdles. In the worst case, every hurdle is cleared and the loop looks at all of them. If an early hurdle is too tall, the method stops sooner.
Space: O(1). Only the loop variable is used, and no copy of the array is made.
Edge cases and common mistakes
- Empty array. The result is
true, because nothing blocks the jumper. - Equal height. A hurdle of 3 with a jump of 3 is cleared. Writing
>=in the test gives the wrong answer here. - Returning
trueinside the loop. If you returntruefor the first hurdle that is low enough, the method would givetruefor[1, 9]with a jump of 5, ignoring the 9. - Checking only the first or last hurdle. The tallest hurdle can be anywhere in the array, so every element needs a look.
- Comparing in the wrong direction. The failing condition is the hurdle being taller than the jump,
height > jumperHeight, not the other way around. - Forgetting the final
return true. The method would give backundefinedwhenever the loop finishes, including for the empty array.
Testing
[1, 2, 3, 4, 5]with a jump of 5 givestrue.[5, 5, 3, 4, 5]with a jump of 3 givesfalse.[5, 4, 5, 6]with a jump of 10 givestrue.[1, 2, 1]with a jump of 1 givesfalse.[]with any jump givestrue.