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!