Chapter 8: Dynamic Programming I
Activity 18: Travel Itinerary
Let's begin by considering the base case and recurrence relation for this problem. Unlike some of the other examples we have discussed in this chapter, this particular problem has just one base case – the point at which the destination has been reached. The intermediate states are also quite simple: given a location at index i
that has a distance limit of x
, we can travel to any location between indices i + 1
and i + x
(inclusive). For example, let's consider the following two cities:
- City 1:
distance[1] = 2
- City 2:
distance[2] = 1
Let's say we wanted to calculate the number of ways to reach the city at index 3
. Because we can reach city 3 from both city 1 and city 2, the number of ways to reach city 3 is equivalent to the sum of the number of ways to reach city 1 and the number of ways to reach city 2. This recurrence is quite similar to the Fibonacci series, except that the...