I have an array of service stations n D[] on the highway, so D[i] is the distance of station i from the beginning of the highway.
I also have an array of costs C[] , so C[i] is the cost of shipping my car to station i.
I need my car to be serviced at the first station, and my car can travel no more than 100 miles between stations.
What is the most efficient algorithm that can be obtained from the beginning of the highway to the end at the lowest cost (I need to know which stations to stop at)? I was able to find a greedy solution to minimize the number of stops, but at the lowest price, I think DP with the optimal subtask:
bestcost[j] = min( 0<i<j bestcost[i] + C[j] st D[j]-D[i] <= 100)
and have a separate array last[j] , which contains the last station at which it stops, which would be the best i from the above subtask.
Would this be the right approach or is there a better Greedy / DP solution?
source share