-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathGas Station.java
31 lines (30 loc) · 984 Bytes
/
Gas Station.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Runtime: 1 ms (Top 100.00%) | Memory: 62.5 MB (Top 91.09%)
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
// *Upvote will be appreciated*
int totalFuel = 0;
int totalCost = 0;
int n = gas.length;
for(int i = 0; i < n; i++) {
totalFuel += gas[i];
}
for(int i = 0; i < n; i++) {
totalCost += cost[i];
}
// if totalfuel < totalCost then It is not possible to tavel
if(totalFuel < totalCost) {
return -1;
}
// It is greather then There may be an Answer
int start = 0;
int currFuel = 0;
for(int i = 0; i < n; i++) {
currFuel += (gas[i]-cost[i]);
if(currFuel < 0) { // It Current Fuel is less than 0 mean we can't star from that index
start = i+1; // so we start from next index
currFuel = 0;
}
}
return start;
}
}