Gas Station(Medium)
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)的更多相关文章
- 【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 ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 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 ...
- 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 ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- 20. Candy && Gas Station
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- LeetCode——Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Gas Station
Description: There are N gas stations along a circular route, where the amount of gas at station i i ...
随机推荐
- 【Python】关于import QtCore报错的处理方法
刚开始学习使用PyQT,但总碰到一些小挫折 比如 import Pyqt成功 而 from PyQt5 import QtCore, QtGui, QtWidgets却报错,找了半天终于找到资料,原因 ...
- Linux相关笔记
vim下 r /etc/hosts 会把这个文件读进来 r! df -Th 会把执行的内容读取进来 查找 / ? 替换:s/old/new/g 2到9行替换2,9s/old/new/g 全部替换 ...
- linux tar/ tar.gz文件解压
1.tar 压缩 tar -cvf jpg.tar *.jpg //将目录里所有jpg文件打包成tar.jpg tar -czf jpg.tar.gz *.jpg //将目录里所有jpg文件打包成 ...
- NetWork--HTTPS 原理解析<转>
转载链接:https://www.cnblogs.com/zery/p/5164795.html HTTPS 原理解析 一 前言 在说HTTPS之前先说说什么是HTTP,HTTP就是我们平时浏览网 ...
- JavaScript—飞机大战2版
前面的思路对了 BUG 出在了计时器和没有加判断页面是否存在元素 <!DOCTYPE html> <html lang="en"> <head> ...
- liunx搭建配置
预安装准备工具 yum -y install gcc gcc-c++ wget cmake 安装 软件存放目录: /lnmp/ 软件安装目录: /usr/local/ 1.下载安装PCRE wget ...
- 你需要知道的 N 种抓取 dump 的工具
原总结注册表debug调试dump转储文件windbgprocdump 前言 今天,向大家介绍几种可以抓取应用程序转储文件的工具及基本使用方法.更详细的用法,请参考每个工具对应的帮助文档.如果你还不清 ...
- JavaSE--[转]加密和签名的区别
转载 http://blog.csdn.net/u012467492/article/details/52034835 私钥用来签名的,公钥用来验签的.公钥加密私钥解密是秘送,私钥加密公钥解密是签名 ...
- 关于shopee平台接口(php)对接示例
2018年8月之后,shopee开始使用新接口,需要进行授权操作 1.授权 public function getAuth(){ /** * @param ShopApiShopee $model * ...
- Hard Disk Drive(MBR)
这里讲的主要是网上所谓的老式磁盘,它是由一个个盘片组成的,我们先从个盘片结构讲起.如图1所示,图中的一圈圈灰色同心圆为一条条磁道,从圆心向外画直线,可以将磁道划分为若干个弧段,每个磁道上一个弧段被称之 ...