18.7 GasUp Problem

#1

I’m a bit confused on the subtraction part. I assume G = gas and D = distance. So why is it G.get(i-1) - D.get(i-1)? The problem stated 20 miles per gallon. Should the distance be divided by 20?

  static int findStartCity(List<Integer> G, List<Integer> D) {
    int carry = 0;
    CityAndRemainingGas min = new CityAndRemainingGas(0, 0);
    for (int i = 1; i < G.size(); ++i) {
      carry += G.get(i - 1) - D.get(i - 1);
      if (carry < min.remainingGas) {
        min = new CityAndRemainingGas(i, carry);
      }
    }
    return min.city;
  }

Thanks!

0 Likes

#2

for the solution write-up, i think they just made D is how much gas you spend (whereas G is how much gas you refill) at each city… and 20 mpg was just an arbitrary value to give an example that they actually didn’t use for the solution write-up.

0 Likes