Gas station
Title description: There are N gas stations on a ring road, and the i-th 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+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.
illustrate:
- If there is a solution to the question, 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.
Please refer to LeetCode official website for example description.
Source: LeetCode
Link: https://leetcode-cn.com/problems/gas-station/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Solution 1: Exhaustive method
Starting from the first gas station, judge whether the current gas station can be used as the starting point to return to the starting point in a circle, if possible, return to the location of the current gas station, if not, the next gas station will be judged as the starting point. The specific process of judging whether a certain gas station can be used as a starting point is as follows:
- If the current fuel quantity plus the gasoline quantity of the current gas station is less than the consumption of the current gas station, it means that you cannot go to the next station and skip this possibility;
- If you can go to the next station, record the current remaining fuel and the number of gas stations that have been passed, then go to the next station and continue to judge;
- Knowing that to the end, if you have walked through all the gas stations, it means that you can circle for a week with the current starting gas station as the starting point and return to the starting gas station.
public class LeetCode_134 {
/**
* 穷举法
*
* @param gas
* @param cost
* @return
*/
public static int canCompleteCircuit(int[] gas, int[] cost) {
// 总共有n个加油站
int totalN = gas.length;
// 从第一个加油站开始遍历
for (int i = 0; i < totalN; i++) {
int gasCount = 0, startStation = i, runStationCount = 0;
boolean flag = true;
while (runStationCount < totalN) {
// 如果当前油量加上当前加油站的汽油量少于当前加油站的消耗,说明无法走到下一站,跳过这种可能性
if (gasCount + gas[startStation] < cost[startStation]) {
flag = false;
break;
} else {
// 剩余油量等于当前油量加上当前加油站的汽油量减去当前加油站的消耗
gasCount = gasCount + gas[startStation] - cost[startStation];
// 走过的加油站
runStationCount++;
// 下个加油站
startStation++;
if (startStation == totalN) {
// 如果下个加油站等于n,从第一个加油站开始
startStation = 0;
}
}
}
if (flag && runStationCount == totalN) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] gas = new int[]{1, 2, 3, 4, 5};
int[] cost = new int[]{3, 4, 5, 1, 2};
System.out.println(canCompleteCircuit(gas, cost));
}
}
【Daily Message】 There is nothing difficult in the world, I am afraid of the people who have a heart. A person will encounter many difficulties in the process of realizing his ideals. It doesn't matter. As long as you have the heart and stick to it, you will succeed.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。