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. 天梯赛 L2-001 紧急救援

    L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道 ...

  2. poj 1144 (Tarjan求割点数量)

    题目链接:http://poj.org/problem?id=1144 描述 一个电话线公司(简称TLC)正在建立一个新的电话线缆网络.他们连接了若干个地点分别从1到N编号.没有两个地点有相同的号码. ...

  3. LINQ的增删改查写法&&组合查询

    .ToList();//返回一个集合,包含查到的所有值: .First();//返回查到的第一条数据,如果查不到会报错: .FirstOrDefault();返回查到的第一条数据,差不到返回一个nul ...

  4. A1014. Waiting in Line

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  5. 【CH0103】最短哈密顿路径

    题目大意:给定一个 N 个点的无向图,点有点权,求从 0 号点走到 N-1 号点的最短哈密顿路径是多少. 题解:由于哈密顿路径的定义是每个顶点必须经过且仅能经过一次,因此,可用当前是否经过了这些点和当 ...

  6. 分布式监控系统Zabbix--使用Grafana进行图形展示

      今天介绍一款高颜值监控绘图工具Grafana,在使用Zabbix监控环境中,通常我们会结合Grafana进行图形展示.Grafana默认没有zabbix作为数据源,需要手动给zabbix安装一个插 ...

  7. flask 连接MogoDB数据库

    # -*- encoding: utf-8 -*- from flask import Flask,request,jsonify,render_template #导入pymongo来连接mongo ...

  8. Mock3 moco框架的http协议post方法Mock的实现

    新建一个 startupPost.json [ { "description":"模拟一个post请求", "request":{ &quo ...

  9. TestNg 5.类分组

    类分组是可以给类去分组,几个类分成不同的组. 比如,建立3个类GroupsOnClass1,GroupsOnClass2,GroupsOnClass3.   GroupsOnClass1和Groups ...

  10. postman 抓包工具charles的使用

    1.直接打开charles,然后,如果有https的话,需要安装证书,然后,设置代理 2.如果不是https的,不需要设置代理,直接抓取就可以 先安装证书: 然后设置代理:               ...