LeetCode 134. 加油站(Gas Station)
题目描述
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -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 升汽油。
因此,无论怎样,你都不可能绕环路行驶一周。
解题思路
从左往右找到第一个加油站汽油量不小于到下一站油耗的站点,从当前站点开始遍历,维护当前剩余的油量,每到一个站点计算剩余油量加加油量是否足以支撑油耗,若不足以支撑到下一站就停止遍历,若最后遍历到原来的站点,说明可以绕环路行驶一周。注意在遍历加油站时,要判断是否走到了末尾站。
代码
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
for(int i = ; i < gas.size(); i++){
if(gas[i] < cost[i]) continue;
int rem = gas[i] - cost[i], j = i == gas.size() - ? : i + ;
while(i != j){
rem += gas[j] - cost[j];
if(rem < ) break;
j = j == gas.size() - ? : j + ;
}
if(i == j) return i;
}
return -;
}
};
LeetCode 134. 加油站(Gas Station)的更多相关文章
- [Leetcode 134]汽车加油站 Gas Station (环形)
[题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i] ...
- LeetCode OJ:Gas Station(加油站问题)
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- LeetCode(134) Gas Station
题目 There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- [Swift]LeetCode134. 加油站 | Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- leetcode 134 加油站问题
leetcode 134 解析 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 co ...
- 【LeetCode练习题】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- [LeetCode 题解]:Gas Station
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 There are ...
- Java实现 LeetCode 134 加油站
134. 加油站 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升 ...
- 【LeetCode OJ】Gas Station
Problem link: http://oj.leetcode.com/problems/gas-station/ We can solve this problem by following al ...
随机推荐
- 【Git的基本操作九】ssh免密登录
SSH免密登录 1. 进入用户家目录 cd ~ 2. 删除原有的 .ssh 目录 rm -r .ssh 3. 运行命令生成 .ssh 目录 ssh-keygen -t rsa -C github或gi ...
- UE中正则表达式
UltraEdit(后简称UE),是我经常使用的文本编辑软件,其功能的强大,令我由衷地爱上了它.每天不用就全身不爽.从最开始的9.0到现在的 12.10a(本人只用到这个版本),UE都是系统重装后必安 ...
- Qt设置按钮为圆形
通过Qt 的样式表实现圆形按钮,其也可以实现圆角按钮,当然也可以使用其他的方式,比如说,通过派生按钮类使用绘图事件,进行一个图形的绘制,或者是通过自定义一个类,通过信号与槽的机制与绘图事件的配合也能实 ...
- 关于单机部署fastdfs遇到的问题
查找错误日志显示:/html/group1/M00/00/00/wKjJWFzdF0qAE1pBAACmOw57Lw0520_big.jpg" failed (2: No such file ...
- 使用ViewFlipper实现图片轮播
public class MainActivity extends AppCompatActivity { private ViewFlipper flipper; //背景图片int[] id pr ...
- VIM技巧----改变
1.大小写转换 ~ 将光标下的字母改变大小写 vaw~ 选中单词(vaw:a会选择一个对象(an object)包括空格在内)后进行大小写转换 viw~ 选中单词(viw:i会选择一个对象的内部(an ...
- Kaggle_泰坦尼克乘客存活预测
转载 逻辑回归应用之Kaggle泰坦尼克之灾 此转载只为保存!!! ————————————————版权声明:本文为CSDN博主「寒小阳」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附 ...
- 第三方库-正则re
Python正则表达式里的单行re.S和多行re.M模式 Python 的re模块内置函数几乎都有一个flags参数,以位运算的方式将多个标志位相加.其中有两个模式:单行(re.DOTALL, 或者r ...
- 在RecyclerView中集成QQ汽泡一
上次已经实现了QQ汽泡的自定义View的效果[http://www.cnblogs.com/webor2006/p/7726174.html],接着再将它应用到列表当中,这样才算得上跟QQ的效果匹配, ...
- crc32 cpp Makefile可参考
https://github.com/stbrumme/crc32 # simple Makefile CPP = g++ # files PROGRAM = Crc32Test LIBS = -lr ...