2019-06-01 17:09:30

问题描述:

问题求解:

其实本题本质上是一个数学题。

【定理】

对于一个循环数组,如果这个数组整体和 SUM >= 0,那么必然可以在数组中找到这么一个元素:从这个数组元素出发,绕数组一圈,能保证累加和一直是出于非负状态。

【证明】

从第一个数字开始进行累加和,中间必然会有一个累加和最低的点,我们设为x。最后的sum >= 0。

现在我们从x出发向后累加,那么必然处于非负状态,并且到了最后一个站点还会有一定的盈余,再从开始向最低点x进发也必然不会出现负数的情况。

【Leetcode Discuss】

If sum of all gas[i]-cost[i] is greater than or equal to 0, then there is a start position you can travel the whole circle.
Let i be the index such that the the partial sum

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]

is the smallest, then the start position should be start=i+1 ( start=0 if i=n-1). Consider any other partial sum, for example,

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]+gas[i+1]-cost[i+1]

Since gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i] is the smallest, we must have

gas[i+1]-cost[i+1]>=0

in order for gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]+gas[i+1]-cost[i+1] to be greater.
The same reasoning gives that

 gas[i+1]-cost[i+1]>=0
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]>=0
.......
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]+...+gas[n-1]-cost[n-1]>=0
 

What about for the partial sums that wraps around?

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[j]-cost[j] + gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1]
>=
gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i] + gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1]
>=0

The last inequality is due to the assumption that the entire sum of gas[k]-cost[k] is greater than or equal to 0.
So we have that all the partial sums

gas[i+1]-cost[i+1]>=0,
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]>=0,
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]+...+gas[n-1]-cost[n-1]>=0,
...
gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1] + gas[0]-cost[0]+gas[1]-cost[1]+...+gas[j]-cost[j]>=0,
...

Thus i+1 is the position to start.

因此,对于本题来说,我们可以计算一下是否总的gas - cost >= 0?如果是,那么必然存在一个解,在这个前提下,只需要遍历一遍数组,如果碰到不能到达的情况,那么就从第一个不能到达的重新开始计算即可。

    public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
int sum = 0;
for (int i = 0; i < n; i++) sum += gas[i] - cost[i];
if (sum < 0) return -1;
int start = 0;
int tank = 0;
for (int i = 0; i < n; i++) {
tank += gas[i];
if (tank < cost[i]) {
start = i + 1;
tank = 0;
}
else {
tank -= cost[i];
}
}
return start;
}

  

加油站问题 Gas Station的更多相关文章

  1. LeetCode 134. 加油站(Gas Station)

    题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...

  2. [Swift]LeetCode134. 加油站 | Gas Station

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

  3. [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

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

  5. [leetcode]134. Gas Station加油站

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

  6. LeetCode OJ:Gas Station(加油站问题)

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

  7. 力扣——gas station (加油站) python实现

    题目描述: 中文: 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] ...

  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. 134. Gas Station加油站

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

随机推荐

  1. 给iPhone找一条平凡之路

    新款iPhone马上就要开发布会了,对于全世界的手机从业者来说,现在的感觉就好像中国人过年:早些时候,大家都盼着过年,放鞭炮.享受美食,小孩子还有压岁钱,但随着生活水平的提高,年味越来越淡,昔日的美好 ...

  2. 自研接口测试平台(Django2+Bootstrap3+Unittest)

    自研接口测试平台(Django2+Bootstrap3+Unittest) HttpRuleTest是一款面向 HTTP 协议的通用测试框架,只需web端维护接口测试数据,即可实现自动化测试.监控.数 ...

  3. 码海拾遗:Linux常用命令(一)

    一.Linux系统安装 系统安装可以分两类:实体机安装Linux,虚拟机(常用虚拟机软件有两种:VMware和VirtualBox)安装Linux. 安装过程网上有很多教程,这里就不赘述了. 二.常用 ...

  4. 分布式系统一致性问题与Raft算法(上)

    最近在做MIT6.824的几个实验,真心觉得每一个做分布式相关开发的程序员都应该去刷一遍(裂墙推荐),肯定能够提高自己的技术认知水平,同时也非常感谢MIT能够把这么好的资源分享出来. 其中第二个实验, ...

  5. EventBus 及一些思考

    EventBus 是 Android 开发的一种常用框架,其解耦的思维令人赞叹 从特性上来讲,其与 Android SDK中的BroadcastReceiver很像,二者都是注册,发送事件,反注册,都 ...

  6. Typora+PicGo+Gitee笔记方案

    前言:需要学习的知识太多,从一开始就在寻找一款能让我完全满意的编辑器,然而一直都没有令我满意的.在前两天Typora新版本更新后,总算是拥有了一套我认为很完美的笔记方案:使用Typora编写markd ...

  7. C#开发BIMFACE系列36 服务端API之:回调机制

    系列目录     [已更新最新开发文章,点击查看详细] 在<C# 开发 BIMFACE 系列文章>中介绍了模型转换.模型对比接口.这2个功能接口比较特殊,发起请求后,逻辑处理是在BIMFA ...

  8. 万字硬核干货!6大技巧,极速提升kubectl的生产力!

    明晚8:30,k3s实战课程开启!将由Rancher研发总监带你畅游k3s与边缘AI的奇妙世界.课程内容完全由实际使用场景中总结而来,别错过啦~!访问以下链接即可传送到课程现场: http://z-m ...

  9. 重置gitlab管理员密码

    gitlab web管理员登录密码忘记以后可以用如下方式修改密码(本演示系统为Linux-CentOS6.6): [root@localhost ~]# gitlab-rails console pr ...

  10. getUserMedia API及HTML5 调用手机摄像头拍照

    getUserMedia API简介 HTML5的getUserMedia API为用户提供访问硬件设备媒体(摄像头.视频.音频.地理位置等)的接口,基于该接口,开发者可以在不依赖任何浏览器插件的条件 ...