Hop Routes Up the Stairs
Easy · 1-D Dynamic Programming · about 15 min
A child climbs a staircase of n steps. In one move they can hop up by any size in the list hops (distinct positive whole numbers). Count the number of different hop sequences that land exactly on step n. Order matters: hopping 1 then 2 is different from hopping 2 then 1.
0 <= n <= 45,1 <= len(hops) <= 10, each hop is between 1 and 10.- For
n = 0the answer is1(the empty sequence). - A hop may not go past step
n.
Examples:
count_hop_routes(4, [1, 2]) → 5 (1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2)
count_hop_routes(1, [2]) → 0
Hints
- Think about the last hop of a route. What step was the child on just before it?
- The count for step i is the sum of the counts for step i minus each hop size that fits.
- The count for step 0 is 1, not 0. Every other count is built from it.
Learn the pattern first: the 1-D Dynamic Programming lesson.
More 1-D Dynamic Programming problems
- Toll Steps to the Top (Easy)
- Stalls Around the Roundabout (Medium)
- Digit Message Decodings (Medium)
- Exact Change Machine (Medium)
- Unspaced Hashtag Split (Medium)
- Best Multiplier Streak (Medium)
- Longest Rising Readings (Hard)