Gas Station leetcode java
题目:
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.
题解:
网上搜到的解法。
据说是Bloomberg的面试题。
用两个变量存+= gas[i] - cost[i]。一个帮助判断当前这个点作为gas station的起点合不合适,一个帮助判断总的需求是不是大于供给。如果总的需求大于供给那么肯定是无解的,如果需求小于等于供给,就可以返回刚才找到的起始点。
代码如下:
1 public int canCompleteCircuit(int[] gas, int[] cost) {
2 if (gas==null|| cost==null||gas.length==0||cost.length==0||gas.length!=cost.length)
3 return -1;
4
5 int sum = 0;
6 int total = 0;
7 int index = 0;
8 for(int i = 0; i < gas.length; i++){
9 sum += gas[i]-cost[i];
total += gas[i]-cost[i];
if(sum < 0){
index=i+1;
sum = 0;
}
}
if(total<0)
return -1;
else
return index;
}
Reference:http://blog.csdn.net/lbyxiafei/article/details/12183461
Gas Station leetcode java的更多相关文章
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
- Gas Station [LeetCode]
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Gas Station——LeetCode
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Gas Station|leetcode 贪心
贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...
- Gas Station [leetcode] 两个解决方案
因为gas的总数大于cost总时间.你将能够圈住整个城市. 第一溶液: 如果一開始有足够的油.从位置i出发.到位置k时剩余的油量为L(i,k). 对随意的k.L(i,k)依据i的不同,仅仅相差常数. ...
- Java for 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 134. Gas Station ----- java
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 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- 漏洞利用查询工具sandi
漏洞利用查询工具sandi 在渗透测试中,一旦发现漏洞,就需要查找该漏洞的利用方式.由于漏洞众多,就需要渗透测试人员从海量的漏洞信息找出可用攻击载荷.Kali Linux内置了一个查询工具sand ...
- android stuidio 导入项目问题。
避免重新下载. === === === 改成自己对应的. ===== Gradle sync failed: Could not find method android() for arguments ...
- Java内存泄露分析和解决方案及Windows自带查看工具
Java内存泄漏是每个Java程序员都会遇到的问题,程序在本地运行一切正常,可是布署到远端就会出现内存无限制的增长,最后系统瘫痪,那么如何最快最好的检测程序的稳定性,防止系统崩盘,作者用自已的亲身经历 ...
- Eclipse添加Spket插件实现ExtJs智能提示
1 . 开发环境 MyEclipse 12.0.0 ExtJs 4.2.1.883 Spket 1.6.23 2 . 下载资源 extjs 4.2.1.883 - http://www.sencha. ...
- VMware安装MikroTik RouterOS chr
简单步骤: 1.官网下载ova镜像 2.导入到vmware即可.
- DevOps 解决方案 - 腾讯云
DevOps 解决方案 - 腾讯云 https://cloud.tencent.com/solution/devops?from=qcloudHpHeaderDevops
- linux无锁化编程--__sync_fetch_and_add系列原子操作函数
linux支持的哪些操作是具有原子特性的?知道这些东西是理解和设计无锁化编程算法的基础. 下面的东西整理自网络.先感谢大家的分享! __sync_fetch_and_add系列的命令,发现这个系列命令 ...
- NoSQL现状
经过了至少4年的激烈争论,现在是对NoSQL的现状做一个阶段性结论的时候了.围绕着NoSQL发生了如此之多的事情,以至于很难对其作出一个简单概括,也很难判断它达到了什么目标以及在什么方面没有达到预期. ...
- System.loadLibrary()的使用方法汇总
当使用System.loadLibrary()调用 Dll,两种方法: 1.设定环境变量. 比如:所编辑的Dll在目录“D:/cppProjects/nativecode/release”内,将这个路 ...
- 好汉两个半第十二季/全集Two and a Half Men迅雷下载
本季Two And A Half Men Season 12 (2014)看点:<好汉两个半>的“半个”是因为第一季播出时杰克年纪太小,只能算半个.故事就在这三个主角和他们周围的女人中发生 ...