Gas Station——又是一道经典问题
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 next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
1 如果总的gas - cost小于零的话,那么没有解返回-1
2 如果前面所有的gas - cost加起来小于零,那么前面所有的点都不能作为出发点。
3 选择最佳出发点后,判断从这点出发能否环行一圈。
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
//vector<int> sum;
int sum=;
int res=-;
for(int i=;i<gas.size();i++)
{
int temp=gas[i]-cost[i];
sum+=temp;
if(res==-&&sum>=)
res=i;
else if(sum<)
{
sum=;
res=-;
}
}
if(res!=-)
{
for(int i=;i<res;i++)
{
int temp=gas[i]-cost[i];
sum+=temp;
if(sum<)
{
res=-;
break;
}
}
}
return res;
}
};
Gas Station——又是一道经典问题的更多相关文章
- [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。
LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...
- 134. Gas Station
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- [leetcode]134. Gas Station加油站
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. Y ...
- [LeetCode 题解]:Gas Station
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 There are ...
- [LeetCode] Gas Station 贪心
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- PAT 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- Leetcode 134 Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
随机推荐
- TYVJ2032 升降梯上
Description: 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘的是一条直通塔顶的轨道.一辆停在轨道底部的电梯.和电梯内一杆控制电梯升降的巨大手柄.Nescafe ...
- 【BZOJ 3811】玛里苟斯 大力观察+期望概率dp+线性基
大力观察:I.从输出精准位数的约束来观察,一定会有猫腻,然后仔细想一想,就会发现输出的时候小数点后面不是.5就是没有 II.从最后答案小于2^63可以看出当k大于等于3的时候就可以直接搜索了 期望概率 ...
- 解决requests获取源代码时中文乱码问题
用requests获取源代码时,如果是中文网页,就可能会出现乱码,下面我以中关村的网站为例: import requests url = 'http://desk.zol.com.cn/meinv/' ...
- input file上传图片预览,非插件
Input标签 <input type="file" name="pic" onchange="changepic(this)" mu ...
- ACE主动对象模式(2)
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/12/590153.html 我们简单的介绍了一下ACE主动对象实现方式,同时提出了两个问题: ...
- 题解【luoguP1351 NOIp提高组2014 联合权值】
题目链接 题意:给定一个无根树,每个点有一个权值.若两个点 \(i,j\) 之间距离为\(2\),则有联合权值 \(w_i \times w_j\).求所有的联合权值的和与最大值 分析: 暴力求,每个 ...
- 数学:Burnside引理与Pólya定理
这个计数定理在考虑对称的计数中非常有用 先给出这个定理的描述,虽然看不太懂: 在一个置换群G={a1,a2,a3……ak}中,把每个置换都写成不相交循环的乘积. 设C1(ak)是在置换ak的作用下不动 ...
- 利用forEach循环Dom元素…
大家都知道forEach是循环数组用的,而且很方便,可以丢掉for循环了,但是它不能循环Dom元素.其实我们可以利用call来完成forEach循环Dom; 假设有这样的HTML结构: <ul ...
- 【bzoj1774-过路费】floyd+排序
题意:n个点,m条双向边,每个点有权值c[i],每条边有权值a[i].d,一条路径的费用=每条边的权值和+各个点的权值的最大值,即sigma(a[i].d)+max(c[i]).q个询问,问x到y的最 ...
- 51nod 1806 wangyurzee的树
基准时间限制:1 秒 空间限制:131072 KB wangyurzee有n个各不相同的节点,编号从1到n.wangyurzee想在它们之间连n-1条边,从而使它们成为一棵树.可是wangyur ...