Suppose you have to drive from Islamabad to Lahore. At the start, your gas tank is full. Your gas tank, when full, holds enough gas to travel m
miles, and you have a map that gives distances between gas stations along the route. Let d1 < d2 < … < dn
be the locations of all the gas stations along the route, where di
is the distance from Islamabad to the gas station. The distance between neighboring gas stations is at most m
miles. Also, the distance between the last gas station and Lahore is at most m
miles.
Your goal is to make as few gas stops as possible along the way. Give a greedy algorithm (in pseudo-code form) to determine at which gas stations you should stop.
Is your solution optimal? What is the time complexity of your solution?