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. 用Emoji和照片挑战大众点评,YOBO玩转新点评方式能引领潮流吗?

    对于一家企业来说,要想获得长久生命力的必备元素是什么?是技术底蕴和海量资金?但诺基亚.摩托罗拉和黑莓等巨头的崩塌,已经证明再稳固的基础都有可能只是沙子做的.是让人工智能.云计算.大数据等前沿技术赋能于 ...

  2. Myeclipse 10/2014 配置插件(svn、maven、properties、velocity)的方法

    一.配置SVN详细图解 什么是SVN? 管理软件开发过程中的版本控制工具. 下面会以两种方式来介绍怎么安装svn,myeclipse安装SVN插件步骤,以myeclipse 2014为例,第一种是最常 ...

  3. Java常用的公共方法

    --获取规字符串中的指定名称的某个字段值 1.public String getValueByName(String params,String name) --用于通过表单选中的复选框获取它的值(j ...

  4. Gson使用指南(一)

    注:此系列基于Gson 2.4. 一.Gson的基本用法 Gson提供了fromJson() 和toJson() 两个直接用于解析和生成的方法,前者实现反序列化,后者实现了序列化.同时每个方法都提供了 ...

  5. ubuntu14.04安装32位库

    sudo dpkg --add-architecture i386 sudo apt update

  6. 画张自己能理解的dotnet core 微服务图

  7. java多线程高并发的学习

    1.      计算机系统 使用高速缓存来作为内存与处理器之间的缓冲,将运算需要用到的数据复制到缓存中,让计算能快速进行:当运算结束后再从缓存同步回内存之中,这样处理器就无需等待缓慢的内存读写了. 缓 ...

  8. ZZJ_淘淘商城项目:day02(淘淘商城01 - 项目讲解、环境搭建)

    在用Eclipse的开发中,可手动排除不必要的依赖坐标传递. <!-- JPA的1.0依赖 --> <dependency> <groupId>javax.pers ...

  9. python摄像头

    import cv2 cap = cv2.VideoCapture(0) i = 0 while 1: ret, frame = cap.read() k = cv2.waitKey(1) if k ...

  10. [GX/GZOI2019]与或和(单调栈+按位运算)

    首先看到与或,很显然想到按照位拆分运算.然后就变成了0/1矩阵,要使矩阵在当前位与为1,则矩阵全为1,如果是或为1,则是矩阵不全为0,然后求全为0/1的矩阵个数即可.记录c[i][j]表示以a[i][ ...