Michael M.

asked • 04/02/22

Dynamic Programming Pseudocode

Suppose you are going on a long bike ride with your e-bike. You must ride exactly n miles. You will need to stop to replace your battery along the way. The bike trail is lined with battery pick-up stations, where you can trade your battery for a new charged battery. There is one battery station at every mile marker. However, the batteries at each station vary. For example, suppose that at station 1 they have batteries that last 3 miles, whereas station 2 may have batteries that last 7 miles. The goal is to complete the bike ride using the minimum number of stops to replace your battery. You cannot run out of power! If you pick up a battery pack worth 4 miles, then you cannot bike more than 4 miles! The input to the problem is an array b[0...n], where b[i] stores the battery life of the batteries at station i. The output is the minimum number of stops required. Assume you start the trail at mile 0, and that you receive the battery at station 0. Provide a dynamic programming solution, in just plain pseudocode, to this problem, and determine the runtime of your algorithm.

1 Expert Answer

By:

Donald W. answered • 04/02/22

Tutor
5.0 (214)

Experienced and Patient Tutor for Math and Computer Science

Bobby J.

What if the question is if it is possible to arrive at n and we are not wasting any battery charges so for any battery you select, you must ride until it is empty before stopping to replace your battery. The input to the problem is an array B[0 . . . n, 1 . . . m], where B[i, j] stores the battery life (in miles) of battery j at station i. For example, station 0 has battery sizes B[0, 1], B[0, 2], . . . , B[0, m]. Assume you start the trail at mile 0, and that you may select to start with any of the batteries at that station. The output to the algorithm should be True if there is a route to mile n, and False otherwise.
Report

04/08/22

Donald W.

It would be quite similar. We would have to change the logic to try all the different batteries at each location rather than just trying every mile between 1 and "charge".
Report

04/08/22

Donald W.

I see that you posted that as a separate question. I just submitted an answer for it.
Report

04/08/22

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.