【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 ...
随机推荐
- 仿新浪微博短网址PHP实现方案
微博限制140字,但是我们知道有时需要分享一个类似淘宝商品的链接,很长,为了避免这个问题,所有了短网址的概念,废话不多说,直接把我的实现方案分享一下: 1)将长网址md5生成32位签名串,分为4段, ...
- Unix/Linux环境C编程入门教程(36) 初识shell
1.什么是Shell Shell是位为一组,依次代表文件拥有者.同组用户和其他用户的存取权限.通常文件共有3个权限,"r"表示只读:"w"表示可写:&qu ...
- ###Git 基础图解、分支图解、全面教程、常用命令###
一.Git 基础图解 转自:http://www.cnblogs.com/yaozhongxiao/p/3811130.html Git 图解剖析 git中文件内容并没有真正存储在索引(.git/in ...
- 喜欢的女生快被别人抢走了,我敢怎么抢? - V2EX
喜欢的女生快被别人抢走了,我敢怎么抢? - V2EX 三大定律镇楼: 第一定律:永远不要以为女生对你有好感.第二定律:告白等于见光死.第三定律:秀恩爱分得快.
- 【转】android 电容屏(一):电容屏基本原理篇
关键词:android 电容屏 tp ITO 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV310(samsung exynos 42 ...
- mysql的wait_timeout配置
mysql数据库有一个wait_timeout的配置,默认值为28800(即8小时). 在默认配置不改变的情况下,如果连续8小时内都没有访问数据库的操作,再次访问mysql数据库的时候,mysql数据 ...
- hpux操作系统的关机与重新启动命令
关机 shutdown -hy 0 重新启动: shutdown -ry 0
- php 中利用json_encode和json_decode传递包括特殊字符的数据
</pre><span style="font-size:24px"></span><pre name="code" ...
- Android应用程序请求SurfaceFlinger服务渲染Surface的过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7932268 在前面一篇文章中,我们分析了And ...
- Visual Studio常用的快捷键
每次在网上搜关于VS有哪些常用快捷键的时候,出来的永远是一串长的不能再长的列表,完全没体现出“常用”二字,每次看完前面几个就看不下去了,相信大家都 有这种感觉.其实我们平时用的真的只有很少的一部分 ...