tags: LeetCode # how are you
Link: https://leetcode-cn.com/problems/gas-station
There are N gas stations on a ring road, and the i-th gas station has gas[i] liters.
You have a car with an unlimited fuel tank. Driving from the i-th gas station to the i+1-th gas station requires gasoline cost[i] liters. You start from one of the gas stations and the gas tank is empty at the beginning.
If you can drive around the loop, return the number of the gas station when you started, otherwise return -1.
Description:
If the question has a solution, that answer is the only answer.
The input arrays are all non-empty arrays and have the same length.
The elements in the input array are all non-negative numbers.
Example 1:
enter:
gas = [1,2,3,4,5]
cost = [3,4,5,1,2]
Output: 3
Explanation:
From 3 Gas station(Index is 3 Place)Get off 4 Litres of gasoline. At this time the fuel tank has = 0 + 4 = 4 Liter of gasoline
Bound for 4 No. gas station, at this time the fuel tank has 4 - 1 + 5 = 8 Liter of gasoline
Bound for 0 No. gas station, at this time the fuel tank has 8 - 2 + 1 = 7 Liter of gasoline
Bound for 1 No. gas station, at this time the fuel tank has 7 - 3 + 2 = 6 Liter of gasoline
Bound for 2 No. gas station, at this time the fuel tank has 6 - 4 + 3 = 5 Liter of gasoline
Bound for 3 No. gas station, you need to consume 5 Litres of gasoline, just enough for you to return to 3 Number gas station.
therefore,3 Can be the starting index.
Example 2:
enter:
gas = [2,3,4]
cost = [3,4,3]
Output: -1
Explanation:
You can't get from 0 Number or 1 Depart from gas station No. because there is not enough gasoline to drive you to the next gas station.
We start from 2 Depart from gas station No., you can get 4 Litres of gasoline. At this time the fuel tank has = 0 + 4 = 4 Liter of gasoline
Bound for 0 No. gas station, at this time the fuel tank has 4 - 3 + 2 = 3 Liter of gasoline
Bound for 1 No. gas station, at this time the fuel tank has 3 - 3 + 3 = 3 Liter of gasoline
You can't go back 2 No. gas station, because the return journey needs to consume 4 Litres of gasoline, but your tank only has 3 Litres of gasoline.
Therefore, no matter what, it is impossible for you to go around the ring road.
Comprehend the question as the following figure. Each node represents the fuel consumption of each station, and each edge represents the fuel consumption to the next station. The question is to ask whether you can start from a node and go back in a clockwise circle To yourself.

Ideas:
Traverse each node and judge whether it can go to the next node based on the current fuel volume and fuel consumption. If it can go to the next node, it will continue to update the fuel consumption until it can't go to the next node. Because it isRingGas station, so the subscript for the next node should beTake the remainder。
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
for (int i = 0; i < n; i++) {
int cur = gas[i];
int j = i;
while (cur - cost[j] >= 0) {
cur += gas[(j+1) % n] - cost[j];
j = (j+1) % n;
if (j == i) {
return i;
}
}
}
return -1;
}
}
During traversaliversusjAn element in the middle of the array pointed to,
*******
^
i
j
If the current fuel volume is greater than the fuel consumption, move backwardj
*******
^^
ij
During the movejWill return to position 0, and the farthest distance that position 0 can reach is 2 (hypothesis), andjIt is still a step-by-step walk. If you can jump directly from 0 to the farthest position you can reach, the time complexity is reduced.
*******
^ ^
j i
In this way, consider recording the farthest position that each vertex can reach, and the remaining oil volume after reaching the farthest position.
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
// record the farthest position that can be reached
int[] lastIndex = new int[n];
Arrays.fill(lastIndex, -1);
// Record the amount of remaining oil reaching the farthest position
int[] remain = new int[n];
for (int i = 0; i < n; i++) {
int cur = gas[i];
int j = i;
while (cur - cost[j] >= 0) {
cur -= cost[j];
j = (j+1) % n;
if (lastIndex[j] != -1) {
cur += remain[j];
j = lastIndex[j];
} else {
cur += gas[j];
}
if (j == i) {
return i;
}
}
lastIndex[i] = j;
remain[i] = cur;
}
return -1;
}
}
If the total fuel consumption is less than the total fuel consumption, no matter which starting point you start, you will never finish a lap.
Assuming fromxThe farthest position that can be reached isy, ThenxToyTotal oilgasDefinitely less than total fuel consumptioncost, AssumingxyThere is a node in betweenz,xCan go to the total oilgas_pre, The fuel consumption iscost_pre, Then fromzToyThe total oil volume isgas-gas_pre, The total fuel consumption iscost-cost_pre,due togas_pre >= cost_pre and sogas-gas_pre < cost-cost_pre, So you cannot go from any node in the middle toy

class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
// Total fuel consumption
int total = 0;
// starting point
int start = 0;
// remaining oil
int cur = 0;
for (int i = 0; i < gas.length; i++) {
cur += gas[i] - cost[i];
if (cur < 0) { //Can't go to the next node
start = i+1;
cur = 0;
}
total += gas[i] - cost[i];
}
return total >= 0 ? start : -1;
}
}
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of ga...
Description of the topic: Method 1: O(n^2) (timeout) Method 2: Official Solution O(n) O(1) Reprinted at: https://www.cnblogs.com/oldby/p/11195617.html...
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of ga...
topic LeetCode - 134. Gas Station Topic Link https://leetcode.com/problems/gas-station/ Reference blog Problem-solving ideas Simple question. Problem-solving Source...
LeetCode: gas station [134] Title Description There on a loopNA gas station, where the firstiGasoline service stationsgas[i] Rise. You have an unlimited capacity fuel tank car, from i Gas station...
gas topic There are N stations in a loop, wherein the i-th gasoline stations Gas [i] l. You have an unlimited capacity of the fuel tank car, from the i-th gas station bound for the first i + 1 gas sta...
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its n...
There are N gas stations along a circular route, where the amount of gas at station i isgas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel fromstation i to its nex...
There are N gas stations on a loop, and the ith gas station has gasoline gas[i] liters. You have a car with an unlimited fuel tank. Driving from the i-th gas station to the i+1th gas station requires ...
Title description On a loopN Gas stations, of whichi Petrol stations have petrolgas[i] Rise. You have a car with unlimited fuel tank capacity, from the firsti Gas stations heading fori+1 Gas stations ...