You and your friends are driving to Tijuana for springbreak. You are saving your money for the trip and so you want to minimize the cost of gas on the way. In order to minimize your gas costs you and your friends have compiled the following information. First your car can reliably travel m miles on a tank of gas (but no further). One of your friends has mined gas-station data off the web and has plotted every gas station along your route along with the price of gas at that gas station. Specifically they have created a list of n+1 gas station prices from closest to furthest and a list of n distances between two adjacent gas stations. Tacoma is gas station number 0 and Tijuana is gas station number n. For convenience they have converted the cost of gas into price per mile traveled in your car. In addition the distance in miles between two adjacent gas-stations has also been calculated. You will begin your journey with a full tank of gas and when you get to Tijuana you will fill up for the return trip. You need to determine which gas stations to stop at to minimize the cost of gas on your trip.
Sample Input:
Prices (cents per mile) [12,14,21,14,17,22,11,16,17,12,30,25,27,24,22,15,24,23,15,21]
Distances (miles) [31,42,31,33,12,34,55,25,34,64,24,13,52,33,23,64,43,25,15]
Your car can travel 170 miles on a tank of gas.
My Output:
Minimun cost for the trip is: $117.35
Gas stations to stop at: [1, 6, 9, 13, 17, 19]
I have already solved the problem, but I am not sure if I have done it the right way. Would someone please give me some suggestion or point me to a right direction if it is wrong? Thank you in advance.
public class GasStation {
/** An array of gas prices.*/
private int[] myPrice;
/** An array of distance between two adjacent gas station.*/
private int[] myDistance;
/** Car's tank capacity.*/
private int myCapacity;
/** List of gas stations to stop at to minimize the cost.*/
private List<Integer> myGasStations;
/**
* A constructor that takes in a price list, distance list, and the car's tank capacity.
* @param thePrice - price list
* @param theDistance - distance list
* @param theCapacity - the car's capacity
*/
public GasStation(final int[] thePrice, final int[] theDistance,
final int theCapacity) {
myPrice = thePrice;
myDistance = theDistance;
myCapacity = theCapacity;
myGasStations = new ArrayList<>();
}
/**
* Calculate for the minimum cost for your trip.
* @return minimum cost
*/
public int calculateMinCost() {
int lenP = myPrice.length;
int lenD = myDistance.length;
if (lenP == 0 || lenD == 0 || myCapacity == 0) return 0;
// gas station -> another gas station (moves)
Map<Integer, Integer> gasD = new HashMap<>();
int[] D = new int[lenD + 1];
D[0] = 0;
// calculate the total distance
for (int i = 0; i < lenD; i++) {
D[i + 1] = D[i] + myDistance[i];
}
int len = D.length;
for (int i = 1; i < len - 1; i++) {
int j = len - 1;
while (D[j] - D[i] >= myCapacity) {
j--;
}
gasD.put(i, j - i);
}
int min = Integer.MAX_VALUE;
for (int i = 1; i < len; i++) {
int temp = 0;
int cur = i;
List<Integer> tempList = new ArrayList<>();
if (D[i] <= myCapacity) {
temp = D[cur] * myPrice[cur];
tempList.add(cur);
int next = gasD.get(cur) + cur;
while (next < len) {
temp += (D[next] - D[cur]) * myPrice[next];
cur = next;
tempList.add(cur);
if (gasD.containsKey(cur)) next = gasD.get(cur) + cur;
else break;
}
if (temp < min) {
min = temp;
myGasStations = tempList;
}
}
}
return min;
}
/**
* Get gas stations to stop at.
* @return a list of gas stations to stop at
*/
public List<Integer> getGasStations() {
return myGasStations;
}
}
11735 cents
for the one-way trip from Tacoma to Tijuana? I got7489 cents
and stops at gas stations[1, 6, 9, 11, 15, 19]
31 miles
atstation 1
for14 cents/mi
, refill152 miles
atstation 6
for11 cents/mi
, refill114 miles
atstation 9
for12 cents/mi
, refill88 miles
atstation 11
for25 cents/mi
, refill121 miles
atstation 15
for15 cents/mi
, and finally arrive atstation 19
(31 * 14) + (152 * 11) + (114 * 12) + (88 * 25) + (121 * 15) = 7489 cents
. and I assumed that we don't need to count the cost of a full tank (which is170 * 12
) atstation 0
(the starting point). well, even if I count it, the cost is still less than yours. If you're curious, I may post a reply below with the method I usedstation 19
for the return trip, it costs147 mi * 21 cents/mi
which is3087
, making total cost of7489 + 3087 = 10576
, again less than your result