Problem:

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.

Solution:

车在 i 站开往 i+1 站,有两种可能:一是车在 i 站加油后所剩油量足够其到达 i+1 站,二则是不足使其到达。

-----------station i-----------------------------------------------station i+1--------

...  pos[i] + gas[i]                     -cost[i]              =pos[i+1] + gas[i+1]  ...

当车从 0 站开往 i+1 站后,剩余油量pos<0,说明当起始站为 0 站,车无法抵达 i+1 站。

得到这一结论后,又可以推出当车从0~i+1之间任何一个站点出发都无法抵达 i+1 站——既然在 i+1 站之前的任何一个站点都有 pos>=0(总油量一直在增加)仍然无法使车到达 i+1,那么在0~i间任何一个站点出发 pos 必然小于等于从 0 出发时的 pos,即更不可能到达 i+1 站。

如此当start∈[0,i],车无法走完全程。探索下一步就要从 i+1 站开始了,按原来方法继续遍历,直到计数 i 不再满足 i<gasSize,就可以返回最终结果了。

Code:

//in C language

int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) {
int start=;//starting station's index
int pos=;//gas's positive accumulation
int neg=;//gas's negative accumulation for(int i=;i<gasSize;i++){
pos+=gas[i]-cost[i];
if(pos<){
neg+=pos;
start=i+;//starts from 0 to i are all impossible.
pos=;
}
} if(pos+neg>=)
return start;
else
return -;
}

最后判断是否能走完全程的if-else语句的解释——最终的neg表示之前所有尝试失败情况下离到达下一站所欠油量的总和,最终的 pos 则是最后一次尝试直到回到最开始的 0 站后所剩的油量。如果这时满足 pos+neg>=0,则从最后一次尝试的起点出发,必然能完整走上一圈,反之则不能。

【LeetCode】134.Gas Station的更多相关文章

  1. [leetcode greedy]134. Gas Station

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

  2. 【LeetCode】Gas Station 解题报告

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

  3. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

  4. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  5. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. bzoj4337树的同构

    树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如果能够把树T1的所有点重 ...

  2. layui记录

    layui 官网 layui 独立版 layui mobile layui 社区

  3. css border 三角形

    当元素的宽高都为0时,只写border,就会发现形成的正方形有4个三角形组成. .triangle { width: 0px; height: 0px; border: 20px solid tran ...

  4. yd的汇总

    因为是我这只蒟蒻个人的汇总嘛,可能有些奇♂怪的东西或者不规范的语言出现啦,见谅见谅 搬了一些到知识汇总里,删了一些过时和无用的,少了好多=.= 1.STL_queue 经实践验证,!qs.empty( ...

  5. HTTPS笔记:使用 SSLEngine 为 aioserver 服务器提供 SSL 访问支持

    现在 HTTPS 的普及率是越来越高,闲来无事,花了二三天时间,为五年前写的 aioserver 服务器提供了 SSL 访问支持. 查看网上资料,为了提高服务器的高并发,建议使用:SSLEngine ...

  6. adb 查看包名或其他

    http://blog.sina.com.cn/s/blog_5b478f870102v5bo.html

  7. HDU 5965 扫雷

    扫雷游戏是晨晨和小璐特别喜欢的智力游戏,她俩最近沉迷其中无法自拔. 该游戏的界面是一个矩阵,矩阵中有些格子中有一个地雷,其余格子中没有地雷. 游戏中,格子可能处于己知和未知的状态.如果一个己知的格子中 ...

  8. CodeForces149D dfs实现区间dp

    http://codeforces.com/problemset/problem/149/D 题意 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选 ...

  9. centos7下安装pip以及mysql等软件

    1.安装pip 安装失败了的提示: No package pip available.Error: Nothing to do 解决方法: 需要先安装扩展源EPEL. EPEL(http://fedo ...

  10. 使用mutt自动发送邮件

    1.Mutt安装及环境配置 1.1.安装 sudo yum install mutt 比如你要设置邮件的发信人,需要做: sudo vim /etc/Muttrc set envelope_from= ...