题目:

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.

地址:

https://oj.leetcode.com/problems/gas-station/

思路:

O(n^2) 的不用闲扯,谁都懂。问题是O(n) 的算法应该如何设计。方法如下:

定义:

存油:卡车带进某个加油站的油量。依题意,初始存油为0.

0、设定初始起点和终点为0.

1、从起点开始,走到不能走为止,设此时不能走的结点为i。则0到i之间的所有结点都不可能为起点。因为从起点开始只有0升油,从起点经过的结点存油可能为正值但至少为0,所以如果以0存油开始,到i一样过不去。

2、起点倒退,追踪i点存油量,直到能过i点为止。

3、回到第一步

证明:

每个结点最多访问一次,O(n)

AC代码:

class Solution {

public:

int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {

int start = 0;

int fin   = 0;

int oil   = 0; // 带入的油

int tmpoil = 0;

int len = gas.size();

if (fin == lastPos(start,len))

return gas[0] >= cost[0] ? 0 : -1;

do

{

int tmpfin = fin; // 我在tmpfin能前进吗?

while (tmpfin != lastPos(start,len) && ((tmpoil = walk2next(oil,tmpfin,gas,cost))) >= 0)

{

tmpfin = nextPos(tmpfin,len);

oil = tmpoil;

}

if (tmpfin == lastPos(start,len))

return start;

fin = tmpfin;

do

{

start = lastPos(start,len);

if (start == fin)

return -1;

int tmp   = gas[start] - cost[start]; // tmp指的是具体某点0油出发到下一点后的剩余油量

tmpoil += tmp;

} while (tmpoil < 0);

oil = tmpoil; // 补到能够进入下一点为止,此时oil为补够了的剩余量

fin = nextPos(fin,len); // 进入下一点

} while (fin != start); // 由于进入了下一点,所以为start的话,就成功。

return start;

}

int lastPos(int index,int len)

{

if (index == 0)

return len - 1;

else

return index - 1;

}

int nextPos(int index,int len)

{

return (index + 1) % len;

}

int walk2next(int oil,int index,vector<int> &gas,vector<int> &cost)

// 带有oil的油量进入index,要走的下个点缺/剩多少油

{

int total = oil + gas[index];

return total - cost[index];

}

};

后记:

当你用while很艰难时,请用do while

【leetCode百题成就】Gas Station解题报告的更多相关文章

  1. 【LeetCode】Gas Station 解题报告

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

  2. LeetCode: Gas Station 解题报告

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

  3. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  4. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  5. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  6. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  8. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  9. 【LeetCode】729. My Calendar I 解题报告

    [LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...

随机推荐

  1. Mybatis自己主动生成代码

    在mybatis自己主动生成代码有两种方式: 方式一:是通过使用eclipse作为开发工具.採用maven来构建项目生成的.以下的演示是通过第一种方式. 今天来记录下mybatis-generator ...

  2. 实时人脸检测 (Real-Time Face Detection)

    源地址:http://blog.sina.com.cn/s/blog_79b67dfe0102uzra.html 最近需要用到人脸检测,于是找了篇引用广泛的论文实现了一下:Robust Real-Ti ...

  3. hadoop集群空间使用情况报告脚本

    近期集群空间有点紧张,总是操心空间不足而崩溃,近期扩容又不太现实,经与集群用户沟通发现:集群上存储了非常多没用的历史数据,能够删除,这样就能够通过一个crontab脚本每天生成集群空间使用报告,当使用 ...

  4. Android照片墙完整版,的完美结合LruCache和DiskLruCache

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/34093441 在上一篇文章其中,我们学习了DiskLruCache的概念和基本使用 ...

  5. make工具与Makefile文件

    make工具与Makefile文件 阅读目录 1. make工具 2. Makefile文件 3. Makefile的简单示例 4. 伪目标 5. Makefile 自动化变量 6. 编译生成多个可执 ...

  6. Rudiments 0.42 发布,C++ 常用工具包 - 开源中国社区

    Rudiments 0.42 发布,C++ 常用工具包 - 开源中国社区 Rudiments 0.42 发布,C++ 常用工具包

  7. 百度地图AP1

    百度地图API的用法 百度地图API演示样例 百度地图API学习总结 <1> <%@ Page Language="C#" Inherits="Syst ...

  8. MSF 离线攻击

    MSF 离线攻击 MSF连环攻击在internet上实现是不太现实的,网络中的安全设备(防火墙.入侵检测.入侵防护系统). 实验拓扑如下: 实验说明:安全实验中的包过滤防火墙在测试中使用的是linux ...

  9. hdu2870(dp求最大子矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2870 分析:分别转换成'a','b','c'三种来求,其实就跟hdu1505一样了... #inclu ...

  10. 升级旧Delphi应用转向支持手机的一个思路

    系统架构改为B/S. 业务规则所有在服务端实现,使用REST服务封装旧有系统,这样可最大程度的利用原有代码. client所实用HTML5+javascript,这样client不须布署PC,可极大减 ...