Gas Station 解答
Problem
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
Solution
Key to the solution is a conclusion:
If sum of gas >= sum of costs, then there must exists one or more solution.
If sum of gas < sum of costs, then there is no solution.
So we can use method of exclusion here.
We need also note here that if A can not reach C in a the sequence of A-->B-->C, then B can not make it either.
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if (gas == null || cost == null)
return -1;
if (gas.length != cost.length)
return -1;
int start = 0;
int sumRemaining = 0;
int totalRemaining = 0;
for (int i = 0; i < gas.length; i++) {
int tmp = gas[i] - cost[i];
if (sumRemaining >= 0) {
sumRemaining += tmp;
} else {
start = i;
sumRemaining = tmp;
}
totalRemaining += tmp;
}
if (totalRemaining < 0)
return -1;
return start;
}
}
Gas Station 解答的更多相关文章
- [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
- 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 加油站问题
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 ...
随机推荐
- 解决Struts2.2.20版本的标签不支持style属性的问题
我先把Exception错误信息贴出来:org.apache.jasper.JasperException: /WEB-INF/jsp/topicAction/addUI.jsp (line: 40, ...
- 不得不看的JVM内存管理
作为一个任何完整的机器都会有内存管理这块组成结构.作为jvm也有自己的内存管理. 1.那么在java中哪些组件需要使用内存. a) Java堆 b) 线程:线程是在jvm运行 ...
- Direct3D 索引缓存
小学的时候我们知道3个顶点组成一个三角形,那么四个顶点我们会说有4个三角形.这就是一个顶点同时参与了四次绘制三角形的结果. 在程序中也一样,比如我们绘制的两个三角形是挨着一起的,总有几个顶点是重合的. ...
- python高级编程(第12章:优化学习)3
#微观剖析 ''' 当找到速度很慢函数时,有时还需要做到测试某个部分函数剖析工作,这需要通过手动对一部分代码速度测试完成 ''' """ import tempfile, ...
- python高级编程 编写一个包1
#目的是:编写,发行python包可重复过程"""1:是缩短开始真正工作之前所需要的设置时间,也就是提供模板2:提供编写包的标准化方法3:简化测试驱动开发方法的使用4:为 ...
- UIScreen类
CGRect screenBounds = [ [UIScreen mainScreen]bounds];//返回的是带有状态栏的Rect NSLog(@"%@", NSStrin ...
- Android 环境配置:git开启多颜色模式
git config --global color.status autogit config --global color.diff autogit config --global color.br ...
- Lua内存泄漏应对方法[转]
转自http://blog.csdn.net/xocoder/article/details/42685685 由于目前正在负责的项目是一个二次开发项目,而且留给我们的代码质量实在让人无力吐槽,所以遇 ...
- nopCommerce架构分析系列(二)数据Cache
原文(http://www.cnblogs.com/gusixing/archive/2012/04/12/2443799.html)非常感谢作者顾思行的分享! 序言 在很多访问量较大的系统中,尤其在 ...
- .net 地址栏传中文乱码 的解决方法
1.设置web.config文件. <system.web> ...... <globalization requestEncoding="gb2312" res ...