|
| 1 | +# [134. Gas Stations](https://leetcode.com/problems/gas-station/) |
| 2 | + |
| 3 | + |
| 4 | +## 题目 |
| 5 | + |
| 6 | +You are given a list of gas stations on a circular route, where the amount of gas at station i is gas[i], and the cost to travel from station i to its next station (i+1) is cost[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. |
| 7 | + |
| 8 | +Write a function that returns the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique. |
| 9 | + |
| 10 | +**Example 1:** |
| 11 | + |
| 12 | +``` |
| 13 | +Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2] |
| 14 | +Output: 3 |
| 15 | +Explanation: Start at station 3 (index 3), you have 4 liters of gas. Now you have a total of =0+4=4 liters of gas. |
| 16 | +Travel to station 4. Now you have 4 - 1 + 5 = 8 liters of gas. |
| 17 | +Travel to station 0. Now you have 8 - 2 + 1 = 7 liters of gas. |
| 18 | +Travel to station 1. Now you have 7 - 3 + 2 = 6 liters of gas. |
| 19 | +Travel to station 2. Now you have 6 - 4 + 3 = 5 liters of gas. |
| 20 | +Travel to station 3 again. This time you will have consumed 5 liters of gas, which is exactly enough for you to return to station 3. |
| 21 | +Therefore, 3 can be the starting index. |
| 22 | +``` |
| 23 | + |
| 24 | +**Example 2:** |
| 25 | + |
| 26 | +``` |
| 27 | +Input: gas = [2,3,4], cost = [3,4,3] |
| 28 | +Output: -1 |
| 29 | +Explanation: You cannot start at station 0 or 1 because there is not enough gas to get you to the next station. |
| 30 | +We start at station 2, we get 4 liters of gas. Now you have a total of =0+4=4 liters of gas. |
| 31 | +Travel to station 0. Now you have 4 - 3 + 2 = 3 liters of gas. |
| 32 | +Travel to station 1. Now you have 3 - 3 + 3 = 3 liters of gas. |
| 33 | +You cannot return to station 2 because it requires 4 liters of gas but you only have 3 liters in your tank. |
| 34 | +Therefore, no matter what, you cannot travel around the ring. |
| 35 | +``` |
| 36 | + |
| 37 | +**Constraints:** |
| 38 | + |
| 39 | +- `gas.length == n` |
| 40 | +- `cost.length == n` |
| 41 | +- `1 <= n <= 10^5` |
| 42 | +- `0 <= gas[i], cost[i] <= 10^4` |
| 43 | + |
| 44 | +## 题目大意 |
| 45 | + |
| 46 | +在一条环路上有 n 个加油站,其中第 i 个加油站有汽油 gas[i] 升。 |
| 47 | + |
| 48 | +你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。 |
| 49 | + |
| 50 | +给定两个整数数组 gas 和 cost ,如果你可以按顺序绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1 。如果存在解,则 保证 它是 唯一 的。 |
| 51 | + |
| 52 | +## 解题思路 |
| 53 | + |
| 54 | +- 初始化总油量(totalGas)和总消耗(totalCost)为0,起始点(start)为0,当前油量(currGas)为0。 |
| 55 | + 遍历每个加油站,累加油量和消耗,计算当前油量。 |
| 56 | + 如果当前油量小于0,说明从上一个加油站到当前加油站的油量不足以到达下一个加油站,因此将起始点设置为当前加油站的下一个位置,并将当前油量重置为0。 |
| 57 | + 遍历结束后,如果总油量小于总消耗,说明无法完成整个旅程,返回-1;否则返回起始点。 |
| 58 | + |
| 59 | +## 代码 |
| 60 | + |
| 61 | +```go |
| 62 | +package leetcode |
| 63 | + |
| 64 | +func canCompleteCircuit(gas []int, cost []int) int { |
| 65 | + totalGas := 0 |
| 66 | + totalCost := 0 |
| 67 | + currGas := 0 |
| 68 | + start := 0 |
| 69 | + |
| 70 | + for i := 0; i < len(gas); i++ { |
| 71 | + totalGas += gas[i] |
| 72 | + totalCost += cost[i] |
| 73 | + currGas += gas[i] - cost[i] |
| 74 | + |
| 75 | + if currGas < 0 { |
| 76 | + start = i + 1 |
| 77 | + currGas = 0 |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if totalGas < totalCost { |
| 82 | + return -1 |
| 83 | + } |
| 84 | + |
| 85 | + return start |
| 86 | +} |
| 87 | + |
| 88 | +``` |
0 commit comments