Start learning free

LESSON 137 OF 415 · LOOPS & CONDITIONALS

Hurdle Jump in Swift: Scanning an Array with an Early Return

Practice Read the full explanation

Given an array of hurdle heights and a jumper's jump height, determine whether or not the hurdler can clear all the hurdles. If they can, return true; otherwise return false.

A hurdler can clear a hurdle if their jump height is greater than or equal to the hurdle height.

Examples:

Input: hurdle heights = [1, 2, 3, 4, 5], jump height = 5
Output: true

Input: hurdle heights = [5, 5, 3, 4, 5], jump height = 3
Output: false

Input: hurdle heights = [5, 4, 5, 6], jump height = 10
Output: true

Input: hurdle heights = [1, 2, 1], jump height = 1
Output: false

Note: Return true for the edge case of an array of hurdles that is empty. (Zero hurdles means that any jump height can clear them).

STEP 1 OF 3
    Ctrl + Enter to check

    SWIFT · LOOPS & CONDITIONALS

    Hurdle Jump in Swift, explained

    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

    1. Loop over the hurdles with for height in hurdleHeights. Each pass gives one hurdle height, and no index is needed.
    2. Return false for a hurdle that is too tall. The test is a strict height > jumperHeight, because a hurdle exactly as tall as the jump can still be cleared. The return ends the whole method, so later hurdles are skipped.
    3. Return true after 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 like Solution().canJump([5, 4, 5, 6], 10).
    • An empty array is fine. for ... in over an empty array runs zero times, so the method reaches return 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 returns true for 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 is true. Using >= in the test would wrongly return false.
    • Empty array. The method returns true.
    • Returning true inside the loop. A hurdle that can be cleared doesn't prove the rest can be. For [1, 2, 1] with a jump of 1, returning true at 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 gives true.
    • [5, 5, 3, 4, 5], 3 gives false.
    • [5, 4, 5, 6], 10 gives true.
    • [1, 2, 1], 1 gives false.
    • [], 0 gives true.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free