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];  
10             total += gas[i]-cost[i];  
11             if(sum < 0){  
12                 index=i+1; 
13                 sum = 0;   
14             } 
15         }  
16         if(total<0)
17             return -1;  
18         else
19             return index;  
20     }

Reference:http://blog.csdn.net/lbyxiafei/article/details/12183461

Gas Station leetcode java,布布扣,bubuko.com

时间: 2024-12-19 11:03:11

Gas Station leetcode java的相关文章

Gas Station——LeetCode

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 e

Gas Station Leetcode Python

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 e

[Lintcode]187. Gas Station/[Leetcode]134. Gas Station

187. Gas Station/134. Gas Station 本题难度: Medium Topic: Greedy Description 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

Gas Station [leetcode] 的两种解法

由于gas总量大于cost总量时,一定可以绕所有城市一圈. 第一种解法: 假设一开始有足够的油,从位置i出发,到位置k时剩余的油量为L(i,k). 对任意的k,L(i,k)根据i的不同,只相差常数. 我们只需要找到最小的L(0, k)对应的k,k+1为所求. 代码如下: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int start = 0; int curGas = 0, minGas

Gas Station [leetcode] 两个解决方案

因为gas的总数大于cost总时间.你将能够圈住整个城市. 第一溶液: 如果一開始有足够的油.从位置i出发.到位置k时剩余的油量为L(i,k). 对随意的k.L(i,k)依据i的不同,仅仅相差常数. 我们仅仅须要找到最小的L(0, k)相应的k,k+1为所求. 代码例如以下: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int start = 0; int curGas = 0, min

[Leetcode][JAVA] Gas Station

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 e

LeetCode: Gas Station 解题报告

Gas Station 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 journ

[C++]LeetCode: 119 Gas Station

题目: 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

Leetcode:Gas Station 加油站

Gas Station 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 journ