加油站问题 Gas Station
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 to0
, then there is a start position you can travel the whole circle.
Leti
be the index such that the the partial sumgas[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
ifi=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 havegas[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 thatgas[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]>=0What 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]
>=0The 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 sumsgas[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的更多相关文章
- LeetCode 134. 加油站(Gas Station)
题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...
- [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 ...
- [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
- [Leetcode 134]汽车加油站 Gas Station (环形)
[题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i] ...
- [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 ...
- 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 ...
- 力扣——gas station (加油站) python实现
题目描述: 中文: 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 134. Gas Station加油站
[抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
随机推荐
- 联想拯救者y7000使用体验
前言 我以前的电脑是在电商平台买的二手电脑,期间觉得软件的运行速度慢,又在网上买了一个128G的固态硬盘安装上.就从大一到大四上学期这么使用了三年半的时间.因为自己需要运行一些吃内存的软件,而我的这个 ...
- 记一个 Base64 有关的 Bug
本文原计划写两部分内容,第一是记录最近遇到的与 Base64 有关的 Bug,第二是 Base64 编码的原理详解.结果写了一半发现,诶?不复杂的一个事儿怎么也要讲这么长?不利于阅读和理解啊(其实是今 ...
- Java基础:浅谈数据输入流/数据输出流《DataInputstream类与DataOutputstream类》
一.理论概述 数据输入/输出流(DataInputStream类与DataOutputStream类) 允许应用程序以与机器无关的方式从底层输入流中读取基本Java数据类型. 说白了就是,当读取一个 ...
- [红日安全]Web安全Day5 - 任意文件上传实战攻防
本文由红日安全成员: MisakiKata 编写,如有不当,还望斧正. 大家好,我们是红日安全-Web安全攻防小组.此项目是关于Web安全的系列文章分享,还包含一个HTB靶场供大家练习,我们给这个项目 ...
- 《前端之路》--- 重温 Egg.js
目录 <前端之路>--- 重温 Egg.js 一.基础功能 > 日志系统包含了 四大层面的 日志对象, 分别是 App Logger.App CoreLogger.Context L ...
- PHP网络爬虫实践:抓取百度搜索结果,并分析数据结构
百度的搜索引擎有反爬虫机制,我先直接用guzzle试试水.代码如下: <?php /** * Created by Benjiemin * Date: 2020/3/5 * Time: 14:5 ...
- Java设计模式二
今天谈的是工厂模式,该模式用于封装和对对象的创建,万物皆对象,那么万物又是产品类,如一个水果厂生产三种水果罐头,我们就可以将这三种水果作为产品类,再定义一个接口用来设定对水果罐头的生成方法,在工厂类中 ...
- Docker部署LAMP项目
前言 之前我们学习了如何在Linux部署LAMP项目,今天我们来学习一下如何在Docker下部署LAMP项项目吧! Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条 ...
- Object-Oriented Programming Summary Ⅳ
目录 UML单元总结博客 总结本单元两次作业的设计 总结自己在四个单元中架构设计以及OO方法理解的演进 总结自己在四个单元中测试理解与实践的演进 总结自己的课程收获 立足于自己的体会给课程组提三个具体 ...
- 从String到==和hashcode
public static void main(String[] args) { String s1 = "ni"; String s2 = "hao"; St ...