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 ...
 
随机推荐
- [HDU4123]Bob’s Race
			
题目大意:给定一棵$n$个点并且有边权的树,每个点的权值为该点能走的最远长度,并输入$m$个询问,每次询问最多有多少个编号连续的点,他们的最大最小点权差小于等于$Q$. 思路:两趟DP(DFS)求出每 ...
 - ==与equals与hashCode的区别联系。
			
前言:对于引用类型的变量,它涉及到两块内存,一块是堆中的内存,用于存放new出来的对象(就是对象的具体内容):另一块是栈内存,用来存放变量在堆内存中的地址. 1,“==” 判断两个变量是否是同一个变量 ...
 - hdu 5772 String problem 最大权闭合子图
			
String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...
 - windows下用nginx配置https服务器
			
1.安装nginx 先到nginx官网下在nginx http://nginx.org/en/download.html 将下载好的文件解压出来修改文件名为 nginx ,然后拷贝到C盘下,目录如下: ...
 - oracle sql 高级
			
1 时间 如果是从当前时间到前一个月的这个时候之间的记录总条数: select count(1) from uis_md_stcustom u where firsttime betw ...
 - 国内打不开onedrive,怎么办?
			
Onedrive不能正常连接使用是由于DNS遭到污染闹的,其上传和下载文件慢也是DNS遭到污染闹的. 方法/步骤 在C盘windows/system32/drivers/etc/hosts下,用记 ...
 - AutoMapper在MVC中的运用03-字典集合、枚举映射,自定义解析器
			
本篇AutoMapper使用场景: ※ 源字典集合转换成目标字典集合 ※ 枚举映射 ※ 自定义解析器 ※ 源中的复杂属性和Get...方法转换成目标属性 源字典集合转换成目标字典集合 □ Domain ...
 - delphi udp文件传输
			
客户端: unit UnitClient; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Contr ...
 - Delphi 判断时间是否合法 -IsValidDateTime、IsValidDate、IsValidTime、IsValidDateDay
			
DateUtils.IsValidDateTimeDateUtils.IsValidDateDateUtils.IsValidTimeDateUtils.IsValidDateDayDateUtils ...
 - DIOCP数据包太大,请在业务层分拆发送
			
DIOCP数据包太大,请在业务层分拆发送 DIOCP日志记录异常:数据包太大,请在业务层分拆发送...... 跟踪发现,原因在下图: