因为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. POJ 1122 FDNY to the Rescue!

    给出某些交叉点的距离,-1 表示无法到达. 然后给出火灾发生点  和 附近的消防局位置. 排列消防局 的 时间 与路径. 反向建图,以火灾出发点为起点做一次SPFA. #include<cstd ...

  2. C语言文件操作之fgets()

        来说一说fgets(..)函数.     原型  char *  fgets(char * s, int n,FILE *stream);     參数:          s: 字符型指针, ...

  3. Factorization Machines 学习笔记(四)学习算法

      近期学习了一种叫做 Factorization Machines(简称 FM)的算法.它可对随意的实值向量进行预測.其主要长处包含: 1) 可用于高度稀疏数据场景:2) 具有线性的计算复杂度.本文 ...

  4. httl开源JAVA模板引擎,动态HTML页面输出

    HTTL(Hyper-Text Template Language)是一个适用于HTML输出的开源JAVA模板引擎,适用于动态HTML页面输出,可用于替代JSP页面,它的指令类似于Velocity. ...

  5. hdu1394(线段树求逆序对)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 线段树功能:update:单点增减 query:区间求和 分析:如果是0到n-1的排列,那么如果 ...

  6. veridata实验举例(6)验证agent启动先后顺序是否对捕获update操作有影响

    veridata实验举例(6)验证agent启动先后顺序是否对捕获update操作有影响 续接veridata实验系列  上篇:"veridata实验举例(5)改动主键上的列值.update ...

  7. Driver 初始化顺序

    Linux系统使用两种方式去加载系统中的模块:动态和静态. 静态加载:将所有模块的程序编译到Linux内核中,由do_initcall函数加载 核心进程(/init/main.c)kernel_ini ...

  8. 从Rational Rose 到IBM Rational Software Architect和IBM Rational Rhapsody

    2014/10/27 RSA升级,重装,不知怎么搞的,不能添加某些图(比如,活动图),重试了几次都不行,在其它电脑上没有问题.后来把其它电脑上的workspace复制过来,问题攻克了,原来是works ...

  9. Python学习入门基础教程(learning Python)--3.3.2 Python的关系运算

    如果if的condition不用布尔表达式来做条件判断而采用关系表达式,实际上关系表达式运算的结果要么是True要么是False.下面我们先了解一些有关关系运算符的基础知识,如下表所示. 做个小程序测 ...

  10. 足球和oracle系列(3):oracle过程排名,世界杯第二回合战罢到来!

    足球与oracle系列(3):oracle进程排名.世界杯次回合即将战罢! 声明:        这不是技术文档,既然学来几招oracle简单招式.就忍不了在人前卖弄几下.纯为茶余饭后与数朋库友的插科 ...