Problem overview
The method canJump receives the heights of the hurdles as an Array[Int] named hurdleHeights, and the jumper's jump height as an Int named jumperHeight. It returns true if the jumper clears 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's height. So a jumper with height 5 clears a hurdle of height 5. An empty array of hurdles counts as cleared, because there is nothing to fail on.
Some examples with their results:
- Hurdles
[1, 2, 3, 4, 5], jump height 5: every hurdle is at most 5, so the result istrue. - Hurdles
[5, 5, 3, 4, 5], jump height 3: the first hurdle is 5, taller than 3, so the result isfalse. - Hurdles
[5, 4, 5, 6], jump height 10: all hurdles are lower, so the result istrue. - Hurdles
[1, 2, 1], jump height 1: the hurdle of height 2 is too tall, so the result isfalse.
Approach: look for one hurdle that fails
"Clears all hurdles" is a statement about every element, and a single counterexample is enough to disprove it. That suggests the same early-exit pattern as a search: scan the hurdles, and the moment one is taller than the jump height, answer false. Only if the whole scan finishes without a failure can the answer be true.
The logic is the mirror image of searching for a value. A search returns true from inside the loop and false after it. This problem returns false from inside the loop and true after it.
Step by step
- Loop over the hurdles.
for (height <- hurdleHeights)gives each hurdle height to the body asheight. - Detect a hurdle that is too tall. The test is
height > jumperHeight, with a strict>. A hurdle exactly as tall as the jump can be cleared, so equality must not count as a failure. When the test is true, returnfalseimmediately. - Return
trueafter the loop. Reaching this line means no hurdle failed the test. For an empty array the loop body never runs, so the method returnstrue, which is what the task wants.
The solution in Scala
class Solution {
def canJump(hurdleHeights: Array[Int], jumperHeight: Int): Boolean = {
for (height <- hurdleHeights) {
if (height > jumperHeight) {
return false
}
}
return true
}
}
Some Scala details:
- The loop variable is a name you choose.
heightis a fresh value on every pass, taken from the array in index order. returnleaves the whole method. It doesn't just end the current pass, so the remaining hurdles are skipped once one has failed.- A deprecation warning. Scala 3.5 compiles this with a warning that a non-local return, meaning a
returninside aforbody, is no longer supported and suggestsboundary.break. The method still works as described. - The method name. The code calls the method
canJump, so that is the name to use when you call it, as innew Solution().canJump(Array(5, 4, 5, 6), 10). - A one-line version.
hurdleHeights.forall(_ <= jumperHeight)expresses the same rule. It is alsotruefor an empty array.
Complexity
Time: O(n), where n is the number of hurdles. In the worst case, every hurdle is checked once. When an early hurdle fails, the loop stops sooner.
Space: O(1). The method uses only the loop variable.
Edge cases and common mistakes
- No hurdles.
canJump(Array(), 0)istrue, since the loop finds nothing to reject. - Equal heights. A hurdle of height 4 and a jump of 4 is cleared, so
canJump(Array(4), 4)istrue. Writingheight >= jumperHeightwould wrongly reject it. - The failing hurdle is last. For
[1, 2, 9]with a jump of 5, the first two hurdles pass and only the last one fails. The method must not decide before the loop has seen it. - Returning
trueinside the loop. Areturn truein anelsebranch would answer after the first hurdle, which is wrong for[1, 9]with a jump of 5. - Swapped comparison. Using
jumperHeight > heightas the failure test reverses the meaning, because that is the condition for a hurdle being cleared.
Testing
canJump(Array(1, 2, 3, 4, 5), 5)givestrue.canJump(Array(5, 5, 3, 4, 5), 3)givesfalse.canJump(Array(5, 4, 5, 6), 10)givestrue.canJump(Array(1, 2, 1), 1)givesfalse.canJump(Array(), 0)givestrue.
