题目:

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. POJ 3040 Allowance 贪心

    这题目的贪心思路还是有一点细节问题的. 还没有证明,据说是因为题目给的条件是每个价格是比它小的价格的倍数才能这么贪心的. 思路如下: 假设要给奶牛的钱为C 1)从大面值到小面值一次拿钱,能拿多少拿多少 ...

  2. 使用国内源解决Qt在线更新慢的问题

    Qt在线安装更新工具默认使用官方的源,国内访问比较慢,可以在setting中增加国内的源来提高更新速度,如下面的源: http://mirrors.ustc.edu.cn/qtproject/onli ...

  3. Codeforces Round #272 (Div. 1)C(字符串DP)

    C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. configure: error: zlib not installed

    export LDFLAGS="-L/usr/local/zlib/lib" export CPPFLAGS="-I/usr/local/zlib/include&quo ...

  5. hdu4004(二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 大致题意 二分最大跳跃能力,判断是否可以在m次内到达对岸! 分析:由于求青蛙最小弹跳能力,所以二 ...

  6. 采用CXF+spring+restful创建一个web接口项目

    这篇文章是http://blog.csdn.net/zxnlmj/article/details/28880303下面,加入的基础上的restful特征 1.参加restful必jar包裹 jsr31 ...

  7. LeetCode——Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

  8. C#文件流写入方法

    stream为服务端接收的文件流 var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的 ...

  9. 【cocos2d-js官方文档】十、log

    api修改情况.左边为新增,右边为原来的. cc.log 不变 cc.warn 新增 cc.error 新增 cc.assert <-- cc.Assert 此次改造有下面几点原因: 加入原来没 ...

  10. 关于split与StringTokenizer的理解

    关于split与StringTokenizer的理解 一.split    依据匹配给定的正則表達式来拆分此字符串.此方法返回的数组包括此字符串的子字符串,每一个子字符串都由还有一个匹配给定表达式的子 ...