题目:

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. Delphi基础Write写入结构体到文件(使用 file of myrecord就行了,真简单)

    program WriteStruct; {$APPTYPE CONSOLE} uses SysUtils; //写入结构体 type TCustomer = record ID: ]; Code: ...

  2. Linux温馨提示1--安装U板块和Windwos划分

    一.安装U盘 现在我用Ubuntu12.04在插入U光盘将被直接安装到/media/下, 10:33linc@Linc-Ubuntu:linc$ df -h Filesystem Size Used ...

  3. 不起眼的 z-index 却能牵扯出这么大的学问(转)

    z-index在日常开发中算是一个比较常用的样式,一般理解就是设置标签在z轴先后顺序,z-index值大的显示在最前面,小的则会被遮挡,是的,z-index的实际作用就是这样. 但是你真的了解z-in ...

  4. 一二三(The Seventh Hunan Collegiate Programming Contest)

    一二三 你弟弟刚刚学会写英语的一(one).二(two)和三(three).他在纸上写了好些一二三,可惜有些字母写错了.已知每个单词最多有一个字母写错了(单词长度肯定不会错),你能认出他写的啥吗? 输 ...

  5. 蓝桥杯 【dp?】.cpp

    题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...

  6. cocos2dx 制作单机麻将(一)

    今天開始打算解说下cocos2dx下怎样制作国标麻将 前半部分先解说麻将的逻辑部分,由于都是代码,可能会比較枯燥无聊. 这部分讲完后,你也能够用其它游戏引擎来制作麻将 后半部分,就解说余下的cocos ...

  7. 32位Linux文件限制大小

    线上程序不断重新启动,查看log发现是进程由于SIGXFSZ信号退出.对过大的文件进行操作的时候会产生此信号,一般仅仅在32位机器上出现,文件限制大小为2G.用lsof查看进程打开的文件,果然有一个文 ...

  8. vim忽略大写和小写查找配置

    作者:zhanhailiang 日期:2014-12-17 默认 vim 的查找是区分大写和小写,可通过下面两种方式实现忽略大写和小写查找 set ic? noignorecase 1 指令设定: : ...

  9. 浅谈JAVA ThreadPoolExecutor(转)

    这篇文章分为两部分,前面是ThreadPoolExecutor的一些基本知识,后一部分则是Mina中一个特殊的ThreadPoolExecutor代码解析.算是我的Java学习笔记吧. 基础 在我看来 ...

  10. HttpMime 处理 多部件 POST 请求

    HttpMime 处理 多部件 POST 请求 在有的场合例如我们要用到上传文件的时候,就不能使用基本的GET请求和POST 请求了,我们要使用多部件的POST请求.由于Android 附带的 Htt ...