标签:style blog http color java os strong io
题目:
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的起点合不合适,一个帮助判断总的需求是不是大于供给。如果总的需求大于供给那么肯定是无解的,如果需求小于等于供给,就可以返回刚才找到的起始点。
代码如下:
Reference:http://blog.csdn.net/lbyxiafei/article/details/12183461
Gas Station leetcode java,布布扣,bubuko.com
标签:style blog http color java os strong io
原文地址:http://www.cnblogs.com/springfor/p/3888036.html