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的更多相关文章

  1. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  2. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  3. [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 ...

  4. 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 ...

  5. 【leetcode】Gas Station

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  6. 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 ...

  7. [LeetCode OJ] Gas Station

    问题描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...

  8. LeetCode _ Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. POJ 2533 Longest Ordered Subsequence - from lanshui_Yang

    题目大意:求一个数列的最长上升子序列(严格上升). 解题思路: 方法一:O(n^2) dp[i]:表示处理到第i个位置,序列的最长上升子序列末尾为i的长度: a[]数组存储原序列 dp[i] = ma ...

  2. Luci流程分析(openwrt下)

    1. 页面请求: 1.1. 代码结构 在openwrt文件系统中,lua语言的代码不要编译,类似一种脚本语言被执行,还有一些uhttpd服务器的主目录,它们是: /www/index.html cgi ...

  3. 如何理解 css3 的 perspective 属性

    一.写在前面的话 最近想多了解一下CSS3的transform 3D效果,transform:英文直译就是转换,它可以实现旋转.缩放.位移等效果,听起来有没有觉得很酷的样子,狠狠的点这里来看看旋转和位 ...

  4. 剑指offer-面试题1:赋值运算符函数

    题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数 class CMyString { public: CMyString(char *pData=NULL); CMyString ...

  5. 为 Python Server Pages 和 Oracle 构建快速 Web 开发环境。

    为 Python Server Pages 和 Oracle 构建快速 Web 开发环境. - 在水一方 - 博客频道 - CSDN.NET 为 Python Server Pages 和 Oracl ...

  6. Restful风格的springMVC配搭ajax请求的小例子

    1. GET请求的例子 ajax代码: 请求参数拼接在url后面(参数在服务器可通过HttpServletRequest获取,也可以直接通过@RequestParam自动注入,参考DELETE例子的方 ...

  7. 修改vim中的tab为4个空格

    记录一下,避免用时还得搜........ 1.临时修改 在vi中,set tabstop=4 或 set ts=4 2.永久修改 vi --version 查看要修改的文件 如果是vim的话,修改~/ ...

  8. [WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored (webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')

    WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored (webxml attribu ...

  9. .NET防止SQL、JS、HTML注入

    /// <summary> /// 过滤标记 /// </summary> /// <param name="NoHTML">包括HTML,脚本 ...

  10. SQL server 2008数据库的备份与还原、分离(转)

    SQL server 2008数据库的备份与还原.分离(转)   一.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Ser ...