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 ...
随机推荐
- 运用Access学习数据库的三大范式
第一范式(1NF):强调的是列的原子性,即“列不能够再分成其他几列”,同一列中不能有多个值. 例子:业余爱好编码表+员工编码表 当员工杨来的业余爱好有多个时,此时的数据库设计不满足第一范式,可进行如下 ...
- ZooKeeper解决的问题
1.解决分布式单点问题 https://www.jianshu.com/p/08b76bd7a634 2.实现分布式环境数据的一致性.访问ZooKeeper树结构时,不同节点返回的数据是一致,不会引起 ...
- PROOF|ADOBE READER
样稿PROOF,最后是印刷样张. 修改校样是最后一次修改错误. 每一版editor不一样,任务不同. 不能修改工作单位,但是可以加一个标注. 最好使用ADOBE READER中的COMMENT& ...
- Django模型基础——(二)
上篇博客主要讲了django中对数据库的增删改查,下面深入再讲解下对数据库的操作. 常用的查询方法 下面以表名为User为例 User.object.first() :返回表中第一条数据 User.o ...
- Dynamics CRM - 不同类型字段在 Plugin 里的赋值方式
在编写 Plugin 代码之前,我们可以需要用 SDK bin 目录下的 CrmSvcUtil.exe 来将 CRM Site 上所有的 Entity 转换成类,而 Entity 里的 Field 也 ...
- 【记录】YAML 简易入门教程
YAML 是 "YAML Ain't a Markup Language"(YAML 不是一种标记语言)的递归缩写.在开发的这种语言时,YAML 的意思其实是:"Yet ...
- thinkcmf2.2 火狐浏览器图片上传以及谷歌图片上传打开稍慢
对目录中 admin/themes/simplebootx/asset/plupload.html 文件 进行更改如下图:
- php抓取网站图片源码
<?php /*完成网页内容捕获功能*/ function get_img_url($site_name){ $site_fd = fopen($site_name, "r&q ...
- Innodb-内存架构与特性
参考文档 Innodb特性buffer_pool http://mysql.taobao.org/monthly/2017/05/01/?spm=a2c4e.11153940.blogcont2812 ...
- 吴裕雄--天生自然 PHP开发学习:表单 - 必需字段
<?php // 定义变量并默认设为空值 $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $ema ...