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. 如何向女朋友介绍MySQL索引

    目录 一.前言 二.正文 三.索引的类型 四.动态查找树 五.B-Tree 1.B-Tree特征 2.B-Tree的查找(select) 3.B-Tree的插入(insert) 4.B-Tree的删除 ...

  2. render_template()的各种用法

    1.可以有很多个参数,第一个一定是模板的名字 2.可以传字典.列表.单个变量等等,还可以传函数,在模板中调用函数 后端函数: from flask import Flask from flask im ...

  3. POJ 1789:Truck History

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21376   Accepted: 8311 De ...

  4. 微信H5支付,成功样例

    <?php/** * Created by PhpStorm. * User: Administrator * Date: 2019/6/3 * Time: 12:00 */ if( !defi ...

  5. Python 学习笔记:Python 使用 pandas 读取数据库并进行绘图

    上次写了在 Python 怎么使用 mssql 库来对 SQL Server 数据库进行增删查改,今天就写一下 Python 如何通过 pandas 来读取数据库并进行绘图. 一.读取数据库: 利用 ...

  6. Spring Cloud Alibaba 教程 | Nacos(三)

    使用Nacos作为配置中心 前面我们已经介绍过滤Nacos是一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平台.所以它可以作为注册中心和配置中心,作为注册中心Nacos可以让我们灵活配置多 ...

  7. selenium滚动条应用,爬永远讲不完的故事

    from selenium import webdriver class Lj(object): def __init__(self): self.driver = webdriver.Chrome( ...

  8. 吴裕雄--天生自然 JAVA开发学习:方法

    /** 返回两个整型变量数据的较大值 */ public static int max(int num1, int num2) { int result; if (num1 > num2) re ...

  9. 14 微服务电商【黑马乐优商城】:day01-springboot(理论篇)

    本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) :day01-springboot(Thyme ...

  10. CSP2019爆零记

    Upd:2019.10.19 初赛 Day 0 CSP-S膜你赛(然而只考一个小时xs) 写(xia)完(xie)有51.5 很虚,很慌 不过CSP-J的模拟有90?(所以CSP-S模拟的码风怎么这么 ...