![在这里插入图片描述](https://img-blog.csdnimg.cn/7bd3cd5ef8154d7da99e5cd9badf12f4.png#pic_center)
🚀题目
在一条环路上有 n 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
给定两个整数数组 gas 和 cost ,如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1 。如果存在解,则 保证 它是 唯一 的。
示例 1:
输入: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
输出: 3
解释:
从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
因此,3 可为起始索引。
示例 2:
输入: gas = [2,3,4], cost = [3,4,3]
输出: -1
解释:
你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
因此,无论怎样,你都不可能绕环路行驶一周。
提示:
gas.length == n
cost.length == n
1 <= n <= 1 0 5 10^5 105
0 <= gas[i], cost[i] <= 1 0 4 10^4 104
💥leetcode代码模板
/**
* @param {number[]} gas
* @param {number[]} cost
* @return {number}
*/
var canCompleteCircuit = function(gas, cost) {
};
🚀思路
方法一:暴力
🟡暴力的解法就是遍历所有的加油站,以每一个加油站作为起点,模拟一圈。如果走了一圈到达了起点且中途油量够用那么就返回该起点的索引,全部遍历完都没找到就返回-1。
当然,暴力的时间复杂度是
O
(
n
2
)
O(n^2)
O(n2),比较耗时,不过用javascript也还能过,没有超时。
方法二:一次遍历
这里参考了leetcode官方的题解,用数学证明来解决,因为有时候直观上并不容易得出某些结论,所以还是得用数学证明。
🟡我们假设从索引x出发最远能到达y,即到达不了y+1,所以油量一定不够,如下式:
∑ i = x y g a s [ i ] < ∑ i = x y c o s t [ i ] \displaystyle\sum_{i=x}^ygas[i] < \displaystyle\sum_{i=x}^ycost[i] i=x∑ygas[i]<i=x∑ycost[i]
既然从x可以到达y,途中必定经过x和y之间的加油站,所以从x出发一定可以到达x和y之间的任意加油站z,用式子表示如下:
∑
i
=
x
z
g
a
s
[
i
]
>
=
∑
i
=
x
z
c
o
s
t
[
i
]
\displaystyle\sum_{i=x}^zgas[i] >= \displaystyle\sum_{i=x}^zcost[i]
i=x∑zgas[i]>=i=x∑zcost[i]
🔵现在我们想知道的是从x和y之间的任意站点z出发能不能到达y的下一个加油站,直观上感觉从x都到不了y的下一个站点,那么从x和y之间的站点出发油更少,应该更加不能到达。但是我们无法确定这个结论,所以要证明一下.如果我们能证明下式,说明上述结论成立:
∑
i
=
z
y
g
a
s
[
i
]
<
∑
i
=
z
y
c
o
s
t
[
i
]
\displaystyle\sum_{i=z}^ygas[i] < \displaystyle\sum_{i=z}^ycost[i]
i=z∑ygas[i]<i=z∑ycost[i]
如果该结论成立,我们就没有必要像方法一样,以x和y之间的站点为起点来模拟一圈,直接从y的下一个站点去模拟就好。
🟢证明如下
∑
i
=
z
y
g
a
s
[
i
]
\displaystyle\sum_{i=z}^ygas[i]
i=z∑ygas[i] =
∑
i
=
x
y
g
a
s
[
i
]
\displaystyle\sum_{i=x}^ygas[i]
i=x∑ygas[i] -
∑
i
=
x
z
−
1
g
a
s
[
i
]
\displaystyle\sum_{i=x}^{z-1}gas[i]
i=x∑z−1gas[i]
<
∑
i
=
x
y
c
o
s
t
[
i
]
\displaystyle\sum_{i=x}^ycost[i]
i=x∑ycost[i] -
∑
i
=
x
z
−
1
c
o
s
t
[
i
]
\displaystyle\sum_{i=x}^{z-1}cost[i]
i=x∑z−1cost[i]
=
∑
i
=
z
y
c
o
s
t
[
i
]
\displaystyle\sum_{i=z}^ycost[i]
i=z∑ycost[i]
得证!
🟣所以我们可以优化一下方法一,即遍历加油站,遇到油不够的情况就直接把下一个加油站作为起点遍历。
💻代码
方法一:暴力
var canCompleteCircuit = function(gas, cost) {
for(let i = 0 ; i < gas.length ; i++){ // 从索引i出发
let rest = gas[i] - cost[i] //记录剩余油量
let index = (i + 1) % gas.length // 往前走一站
while(rest > 0 && index !== i){ // 只要还有油且没回到起点
rest += gas[index] - cost[index]
index = (index + 1) % gas.length
}
if(rest >= 0 && index === i) return i
}
return -1
};
方法二:一次遍历
var canCompleteCircuit = function(gas, cost) {
const n = gas.length;
let i = 0;
while (i < n) {
let sumOfGas = 0, sumOfCost = 0;
let cnt = 0;
while (cnt < n) {
const j = (i + cnt) % n;
sumOfGas += gas[j];
sumOfCost += cost[j];
if (sumOfCost > sumOfGas) {
break;
}
cnt++;
}
if (cnt === n) {
return i;
} else {
i = i + cnt + 1;
}
}
return -1;
};
✨ 我 是 c o n e r , 请 别 关 注 我 的 专 栏 , 本 系 列 文 章 为 个 人 刷 题 记 录 ( 偷 偷 自 己 卷 🤤 ) : \textcolor{green}{我是coner,请别关注我的专栏,本系列文章为个人刷题记录(偷偷自己卷🤤):} 我是coner,请别关注我的专栏,本系列文章为个人刷题记录(偷偷自己卷🤤):leetcode题解js
✨ 每 天 早 上 更 新 3 道 l e e t c o d e 题 目 的 j s 题 解 🚀 🚀 🚀 \textcolor{green}{每天早上更新3道leetcode题目的js题解🚀🚀🚀} 每天早上更新3道leetcode题目的js题解🚀🚀🚀