Problem overview
The method CanJump(int[] hurdleHeights, int jumperHeight) 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 jump of 5 clears a hurdle of height 5.
The examples, written as {hurdles}, jump:
{1, 2, 3, 4, 5}, 5givestrue, since no hurdle is taller than 5.{5, 5, 3, 4, 5}, 3givesfalse: the first hurdle is 5, which is more than 3.{5, 4, 5, 6}, 10givestrue.{1, 2, 1}, 1givesfalse, because of the hurdle of height 2.
With no hurdles at all, the answer is true: nothing stands in the jumper's way.
Approach: look for a hurdle that blocks
"Clears all hurdles" means "no hurdle is too tall". It is easier to search for the opposite, a hurdle that fails, than to confirm that every hurdle passes. As soon as one hurdle is taller than the jump height, the final answer is false, and the rest of the array doesn't matter. If the scan finishes without finding such a hurdle, the answer is true.
This is the "for all" version of a pattern you may have already used for "does any element match?". There you return true on the first match and false at the end. Here the roles are swapped: return false on the first failure and true at the end.
Step by step
- Loop over the heights.
foreach (int height in hurdleHeights)gives each hurdle in turn, with no index needed. - Test for a failure and return at once. A hurdle blocks the jumper when
height > jumperHeight. The test is a strict "greater than", because a hurdle of exactly the jump height can still be cleared. Returningfalseimmediately ends the method, so later hurdles are skipped. - Return
trueafter the loop. Reaching it means no hurdle was too tall. An empty array skips the loop entirely and gets this answer, which is what the task asks for.
The solution in C#
class Solution {
public bool CanJump(int[] hurdleHeights, int jumperHeight) {
foreach (int height in hurdleHeights) {
if (height > jumperHeight) {
return false;
}
}
return true;
}
}
Tracing {5, 5, 3, 4, 5} with a jump of 3: the first height is 5, and 5 > 3 is true, so the method returns false before it sees the other four hurdles. Tracing {1, 2, 1} with a jump of 1: 1 > 1 is false, 2 > 1 is true, so it returns false on the second hurdle.
C# details:
- The method name is
CanJump, in PascalCase, and the return type isbool. Code ported from other languages often uses lowercase names such ascanJump, which is a different name in C#. - Every path returns. The compiler demands a return value on every route. The final
return true;handles the route where the loop completes. - The loop variable is read-only. Inside
foreachyou can readheightbut not assign to it.
Complexity
Time: O(n), where n is the number of hurdles. The worst case, when every hurdle can be cleared, looks at all of them once. When a tall hurdle comes early the method finishes sooner, but the worst case decides the bound.
Space: O(1). Only the loop variable is used.
Edge cases and common mistakes
- Using
>=in the test. That treats a hurdle equal to the jump height as a failure.{1, 2, 3, 4, 5}with a jump of 5 would wrongly returnfalse. - Using
<and returningtrueinside the loop. That answers after the first hurdle that can be cleared, ignoring the ones after it. - Forgetting the empty array. The reference handles it without any extra code. An early
if (hurdleHeights.Length == 0) return false;would be wrong. - Comparing the wrong direction.
jumperHeight > heightin the failing test mixes up the roles and answers a different question. - A single hurdle.
{3}with a jump of 3 givestrue;{4}with a jump of 3 givesfalse.
Testing
{1, 2, 3, 4, 5},5givestrue.{5, 5, 3, 4, 5},3givesfalse.{5, 4, 5, 6},10givestrue.{1, 2, 1},1givesfalse.{},0givestrue.