Stepping Stones Across the Pond
A pond has a straight line of n stepping stones, and the last one touches the far bank. A child crosses from the near bank one move at a time. Each move is either a step onto the next stone or a hop that skips one stone and lands on the one after.
She wants to cross a different way every day, so she needs to know how many different crossings exist.
Your task
Write countCrossings(n). Return the number of different sequences of steps and hops that start on the near bank and end exactly on stone n.
The near bank is position 0 and the stones are positions 1 to n. A step moves 1 position, a hop moves 2. Two crossings differ if their sequences of moves differ. She may not hop past stone n.
countCrossings(n: int) → int
Examples
n = 3
3
The three crossings are step-step-step, step-hop and hop-step. Step-hop and hop-step land on different stones on the way, so they count as two different crossings.
n = 5
8
With no hops there is only one crossing, five steps. With one hop there are 4 moves in total and the hop can be any of them, which gives 4 crossings. With two hops there are 3 moves and the single step can be first, second or third, which gives 3 crossings. Three hops would cover 6 positions, which is past the last stone. The total is 1 + 4 + 3 = 8.
Constraints
- 1 ≤ n ≤ 70
- The answer fits in a normal integer (it is below 10¹⁵ for n = 70).
Start with brute force
Write the rule recursively: count(i) = count(i - 1) + count(i - 2), stopping at the bank. This is correct and short, but it is slow.
The running time is exponential, because count(70) calls count(69) and count(68), and count(69) calls count(68) again. The number of calls grows like the answer itself, hundreds of trillions for n = 70.
Hints
0 of 4 shown- Hint 1
Look only at the last move onto stone 5. Where could she have been just before it?
- Hint 2
She was either on stone 4 and took a step, or on stone 3 and took a hop. The two groups never overlap, so the total is the number of ways to reach stone 4 plus the number of ways to reach stone 3.
- Hint 3
That holds for every stone:
ways(i) = ways(i - 1) + ways(i - 2). The starting values are 1 way to be on the bank (do nothing) and 1 way to reach stone 1. - Hint 4
Fill the answers from stone 1 upward instead of recursing down. Each value needs only the two before it, so two variables are enough. Plain recursion recomputes the same stones trillions of times for n = 70.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Count by the last move: every crossing that ends on stone i either stepped from i - 1 or hopped from i - 2. So ways(i) = ways(i - 1) + ways(i - 2).
Fill a small table for n = 5, left to right:
- Bank: 1 way (you are already there). Stone 1: 1 way (one step).
- Stone 2: 1 + 1 = 2 (step-step, hop).
- Stone 3: 2 + 1 = 3.
- Stone 4: 3 + 2 = 5.
- Stone 5: 5 + 3 = 8.
Each entry is computed once from two you already have, unlike the recursion, which recomputes stone 3 many times. Since each step looks only two entries back, two variables replace the table.
function countCrossings(n) {
let twoBack = 1; // ways to be on the bank (position 0)
let oneBack = 1; // ways to be on stone 1
for (let i = 2; i <= n; i++) {
const here = oneBack + twoBack; // last move was a step or a hop
twoBack = oneBack;
oneBack = here;
}
return oneBack;
}def count_crossings(n):
two_back = 1 # ways to be on the bank (position 0)
one_back = 1 # ways to be on stone 1
for _ in range(2, n + 1):
# last move was a step (from i - 1) or a hop (from i - 2)
two_back, one_back = one_back, one_back + two_back
return one_backRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.