1.在做pat的to fill or not to fill的时候想起同样是加油站的题目,于是翻出来复习一下

2.关键在于理解潜在的条件。假设油量为tank,如果到了当前站i,tank<0,即不能到达站i+1,那么起始站start和i之间的任何一个站i都不能到达站i+1。因为每个站至少贡献了0或者>0的油量,去掉一个站,那么tank必然比现在的油量还要小,所以更加不可能达到i+1.

证明:

设tank[a,b]是指以a为起始站,到达b站后,油箱的油量。

如果上述2结论不成立,则可以推导出,在start与i之间的某一个站j,使得tank[j,i]>tank[start,i],又tank[start,i]=tank[start,j]+tank[j,i],那么推得tank[start,j]<0,

而车能够从start走到i,所以对于任意k属于[start,i],均有tank[start,k]>=0,与tank[start,j]<0矛盾,所以2结论是正确的。

AC代码:

class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int tank=0;//当前的油箱油量
int start=0;//从第0个站开始计算,后面也作为结果进行返回
int n=gas.size();
for(int i=0;i<gas.size();i++)
{//遍历所有加油站
tank+=gas[(i+start)%n]-cost[(i+start)%n];//更新油箱的油量
if(tank<0)
{//如果tank小于0,表明没办法从i走到下一个油站,那么start直接从下一站开始
//如果无法从start达到i,那么start和i之间任何一个站都不能达到i,因为每个站至少贡献了0和>=0的油量
start=i+start+1;//start从当前i站的下一个站开始
i=-1;//使得下次从i=0开始
if(start>=n)
{//已经把所有情况都遍历了,仍不能满足要求
return -1;
}
tank=0;
}
}
return start;
}
};

之前的AC代码:

class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int tank=0;
int start=0;
int n=gas.size();
for(int i=0;i<n+start;i++)
{
tank=tank+gas[i%n]-cost[i%n];
if(tank<0)
{//如果油箱汽油小于0,则不能到达,即从start无法到达i,start与i之间的任何一站都不可能达到i
//因为从start到i这个过程中,每一站至少贡献0的油量,假设去掉start,则tank会减去0或者一个正数,导致更加不可能到达i
start=i+1;//从下一个站继续开始重新计算
if(start>=n)//如果下一个站已经超过了n,证明前n个站都不可能完成循环,所以return-1
return -1;
tank=0;
}
}
return start;//如果循环结束后tank大于等于0,则能够达到目的地
}
};

Gas Station(Medium)的更多相关文章

  1. 【Leetcode】【Medium】Gas Station

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

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

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

  3. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  4. 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 ...

  5. 【leetcode】Gas Station

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

  6. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  7. 20. Candy && Gas Station

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. LeetCode——Gas Station

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

  9. Gas Station

    Description: There are N gas stations along a circular route, where the amount of gas at station i i ...

随机推荐

  1. 吴裕雄--天生自然 JAVASCRIPT开发学习:prototype(原型对象)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Linux--shell 脚本免密码输入

    参考:https://www.cnblogs.com/lixigang/articles/4849527.html #!/bin/bash ssb=" password=mysql data ...

  3. vue打包空白及字体路径错误问题

    vue项目打包后空白 在config/index.js文件中 assetsPublicPath: '/', 改为 assetsPublicPath: './', build: { // Templat ...

  4. MAVEN报错Could not get the value for parameter compilerId for

    Maven:Could not get the value for parameter compilerId for plugin execution default-compile..... 前两天 ...

  5. CENTOS YUM更新源

    网络yum源和制作本地光盘yum源 配置CENTOS YUM更新源 yum安装rpm包安装后本地不清除的方法 sed -i 's#keepcache=0#keepcache=1#g' /etc/yum ...

  6. h5-其他伪元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Python实现Collatz序列(考拉兹猜想)

    考拉兹猜想(英语:Collatz conjecture),又称为奇偶归一猜想.3n+1猜想.冰雹猜想.角谷猜想.哈塞猜想.乌拉姆猜想或叙拉古猜想,是指对于每一个正整数,如果它是奇数,则对它乘3再加1, ...

  8. 《Docekr入门学习篇》——Docker常用命令

    Docker常用命令 Docker镜像管理 搜索镜像:docker search 获取镜像:docker pull 查看镜像:docker images 删除镜像:docker rmi 构建镜像:do ...

  9. UVA 10273

    我是用暴力过的,虽然网上说刘汝佳出的这道题考的是堆,我不太懂,..用暴力时间复杂度高一些,但是一样能过 所要注意的就是周期问题,因为只要同时存在某一天超过一头牛产奶量最小,就不会杀牛,而每头牛的周期和 ...

  10. vim,neovim 配置文件

    插件管理用的是 https://github.com/junegunn/vim-plug 打开网址,会有示例,如何安装它本身,以及一些插件,照做,很容易完成 里面有两个可用的配置文件,cp_vimrc ...