因为gas的总数大于cost总时间。你将能够圈住整个城市。

第一溶液:

如果一開始有足够的油。从位置i出发。到位置k时剩余的油量为L(i,k)。

对随意的k。L(i,k)依据i的不同,仅仅相差常数。

我们仅仅须要找到最小的L(0, k)相应的k,k+1为所求。

代码例如以下:

    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int start = 0;
int curGas = 0, minGas = 0, totalGas = 0;
for (int i = 0; i < gas.size(); i++)
{
int temp = gas[i] - cost[i];
curGas += temp;
totalGas += temp;
if (minGas > curGas)
{
start = i + 1;
minGas = curGas;
}
}
if (totalGas >= 0) return start % gas.size();
else return -1;
}

另外一种解法:

假设L(i,k) < 0,则从i和k之间全部的位置都不能到k

所以从k+1的位置从0開始找

    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int start = 0;
int curGas = 0, totalGas = 0;
for (int i = 0; i < gas.size(); i++)
{
int temp = gas[i] - cost[i];
curGas += temp;
totalGas += temp;
if (curGas < 0)
{
start = i + 1;
curGas = 0;
}
}
if (totalGas >= 0) return start % gas.size();
else return -1;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Gas Station [leetcode] 两个解决方案的更多相关文章

  1. Gas Station leetcode java

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

  2. 134. Gas Station leetcode

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

  3. Gas Station [LeetCode]

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

  4. Gas Station——LeetCode

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

  5. Gas Station|leetcode 贪心

    贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...

  6. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  7. [LeetCode] 134. Gas Station 解题思路

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

  8. [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  9. [LeetCode] Gas Station 加油站问题

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

随机推荐

  1. A Game of Thrones(15) - Sansa

    Eddard Stark had left before dawn, Septa Mordane informed Sansa as they broke their fast. “The king ...

  2. CF 518D(概率dp)

    传送门:Ilya and Escalator 题意:有n个人排队进车厢,每秒只能进一个人,而且第1个人进了后面的人才能进,第一个人每秒进入车厢的概率为p,不进的概率为1-p,求t秒后进入车厢总人数的数 ...

  3. Effective C++ -- 构造析构赋值运算

    05.了解C++默默编写并调用哪些函数 编译产生的析构函数时non-virtual,除非这个类的基类析构函数为virtual 成员变量中有引用和const成员时,无法自己主动生成copy assign ...

  4. fiddler4使用教程(转)

    Fiddler的基本介绍 Fiddler的官方网站:  www.fiddler2.com Fiddler官方网站提供了大量的帮助文档和视频教程, 这是学习Fiddler的最好资料. Fiddler是最 ...

  5. hdu4614(线段树+二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意:给定一个区间[0,N-1],初始时每个位置上的数字都是0,可以对其进行以下两种操作: 1. ...

  6. 从mina中学习超时程序编写

    从mina中学习超时程序编写 在很多情况下,程序需要使用计时器定,在指定的时间内检查连接过期.例如,要实现一个mqtt服务,为了保证QOS,在服务端发送消息后,需要等待客户端的ack,确保客户端接收到 ...

  7. osx下快捷键相应符号

    2张图展示mac下相应的按键符号: 很多其它文章请前往小胖轩.

  8. Leetcode 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

  9. VC6.0入门使用

    软件下载地址 http://pan.baidu.com/s/1qWuqFAO 新建win console 32 project,然后新建header文件.最后新建source cpp文件.如图所看到的

  10. Unity3D开发一个2D横版射击游戏

    教程基于http://pixelnest.io/tutorials/2d-game-unity/ , 这个例子感觉还是比较经典的, 网上转载的也比较多. 刚好最近也在学习U3D, 做的过程中自己又修改 ...