【LeetCode练习题】Gas Station
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.
题目意思:
有N个加油站,gas[i]表示第i个加油站有多少汽油,cost[i]表示从第i个加油站去往第i+1个加油站要消耗多少汽油。开始的时候汽车里面没有汽油。
返回某个(从0开始的)加油站,从这个加油站出发可以环绕N个加油站一周一直都有油。如果找不到,返回-1。
解题思路:
假设edge[i] = gas[i] - cost[i]。那么若edge[0] > 0,表示从0号加油站可以到达1号加油站。
显然,如果edge[0]+edge[1]+edge[2]+edge[3]+edge[4]+edge[5]+edge[6]+edge[7]+edge[8]+edge[9] < 0,应该返回-1。
我们考虑这样一种情况,从0号出发,0号可以到1号,然后到2号,然后到3号,当到达3号的时候我们发现0,1,2,3号加油站目前为止的gas[0],gas[1],gas[2],gas[3]的总和开始比cost[0],cost[1],cost[2],cost[3]小了,即edge[0]+edge[1]+edge[2]+edge[3] < 0。此时的油量将无法支撑我们开往4号加油站。
这样的话,可以得出0号是不能作为起点的结论。
那么1号可以作为起点吗?
显然也不能!
因为edge[0]+edge[1]+edge[2]+edge[3] < 0,而且我们已经知道了edge[0] > 0,那么很自然的可以知道edge[1]+edge[2]+edge[3] < 0必定成立,这样从1号开始出发的话,结局也一定是在3号加油站这里悲剧。
那么2号,3号呢?一样也不能作为起点了。
所以,我们应该直接考虑3号的下一个加油站4号作为新的起点,假设一共有10个加油站(0号到9号),当以4号为新起点的时候,如果一直遍历到9号,都没有出现问题,即
edge[4]+edge[5]+edge[6]+edge[7]+edge[8]+edge[9] > 0,那么4号就是我们返回的结果了。
代码如下:
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int left=;
int totalLeft = ;
int start = ;
int len = gas.size();
for(int i = ; i < len; i++){
left += gas[i] - cost[i];
totalLeft += gas[i] - cost[i];
if(left < ){
start = i + ;
left = ;
}
}
return totalLeft>=? start:-;
}
};
【LeetCode练习题】Gas Station的更多相关文章
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- [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 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 ...
- leetcode 134. Gas Station ----- java
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode OJ] Gas Station
问题描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- 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]134. Gas Station加油站
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. Y ...
- Java for 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 ...
随机推荐
- Android EditText setOnClickListener事件 只有获取焦点才能响应 采用setOnTouchListener解决
最近在学习Android开发,在编写程序的过程中,发现EditText setOnClickListener事件响应中,只有获取焦点的时候才会响应, 如当焦点在别的控件上时,只能先点击获取焦点,第二次 ...
- [Ubuntu]在Ubuntu下搭建自己的源服务器
1.摘要 网上有很很多关于搭建源镜像的文章,但是对于一般人来讲,用不着镜像所有的deb包,只对我们能用到的感兴趣.另外,对于一些在Ubuntu的源中没有的软件,我们也可以放在我们自己的源里,这 ...
- 用于下载AD官网登录账号:User name: fuxin918@fuxin918.com Passeword: s6c0W1w8
用于下载AD官网登录账号:User name: fuxin918@fuxin918.com Passeword: s6c0W1w8
- Php环境下载(PHPNow)安装
下载 From http://servkit.org/download 安装 解压下载包,双击setup.cmd,按照提示执行安装. 安装成功测试 原来的解压目录
- fullcalender
http://blog.csdn.net/francislaw/article/details/7740630 引用 <link rel="stylesheet" href= ...
- java打印菱形的简单方法
代码: public class Diamond { /** * 测试main方法 */ public static void main(String[] args) { printDiamond(1 ...
- navigationBar 背景色
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0) { [[UINavigationBar appearance] s ...
- FreeMarker的模板文件语法
FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1,文本:直接输出的部分 2,注释:<#-- ... -->格式部分,不会输 ...
- struts2采用convention-plugin实现零配置
最近开始关注struts2的新特性,从这个版本开始,Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置. 配置文件精简了,的确是简便 ...
- 【C语言】输入一组整数,求出这组数字子序列和中最大值
//输入一组整数.求出这组数字子序列和中最大值 #include <stdio.h> int MAxSum(int arr[],int len) { int maxsum = 0; int ...