You are viewing a single comment's thread. Return to all comments →
We have done it in O(n) whithout any extra memory.
static int truckTour(int[][] petrolpumps) { long sum = 0; int index = 0; boolean isIndexSet = false; for (int i = 0; i < petrolpumps.length; i++) { int currFuel = petrolpumps[i][0]; int currDist = petrolpumps[i][1]; int diff = currFuel - currDist; sum += diff; if (sum < 0) { isIndexSet = false; sum = 0; } else { if (!isIndexSet) { isIndexSet = true; index = i; } } } return index; }
Seems like cookies are disabled on this browser, please enable them to open this website
Truck Tour
You are viewing a single comment's thread. Return to all comments →
We have done it in O(n) whithout any extra memory.